开发者

In Rails, how do you call a specific method in a controller from a form in a view?

new to rails so bear with me. What I would like to do, is I have a list of messages on the screen. I want the message to be marked as 'closed' which means it just fills in a completeDate.

I have a model:

class Message < ActiveRecord::Base
  attr_accessible :topic, :body, :completeDate, :created_at
  belongs_to :account
  belongs_to :user
  has_many :message_followups
end

I have a controller:

class MessagesController < ApplicationController
def开发者_如何学Python close
   @message = Message.find(params[:messageId])
   @message.completeDate = Date.today
   if @message.save
    redirect_to myHomeMessages_path, :flash => { :success => "Your message was closed." }
   else
    redirect_to myHomeMessages_path, :flash => { :error => "Error closing message." }
   end
end
end

I have a view:

<%= form_for (???) do |f| %>
    Are you sure you wish to close this message?<br>
    <%= hidden_field_tag 'messageId', message.id.to_s %>
    <%= submit_tag "Close Message" %>
<% end %>

I'm having a problem figuring out how to get the form_for or a form_tag to call the specific method 'close' in the messages controller. Any help would be greatly appreciated, thanks.

Matt


A very important hint, you should not use camel case for variable names on Ruby, variable sand methods should use underscore as separators, if you're coming from another language like Java, try to avoid using the same naming patterns you used in there. Camel case in ruby is only for class and module names.

I am guessing you're using Ruby on Rails 3, as you haven't said what you're using. First, you need a route for that, it would look like this on your routes.rb file:

resources :messages do
    member do 
        post 'close'
    end
end

A little change to your controller

def close
   @message = Message.find(params[:id]) #you don't need to use the hidden field here
   @message.completeDate = Date.today
   if @message.save
    redirect_to myHomeMessages_path, :flash => { :success => "Your message was closed." }
   else
    redirect_to myHomeMessages_path, :flash => { :error => "Error closing message." }
   end
end

And your form is going to look like this:

<%= form_for( @message, :url => close_message_path( @message ), :html => { :method => :post } ) do |f| %>
    Are you sure you wish to close this message?<br>
    <%= f.submit "Close Message" %>
<% end %>

This form is going to be posted to "/messages/ID_HERE/close" and Rails is going to set the "ID_HERE" value as the "id" parameter on your request.

You can see the full documentation about form_for here.


Something like this should square things away for you:

<%= form_for @message, :url => { :action => "close" }, :html => { :method => :put } do |f| %>

What this does is specify that you are performing an HTTP PUT method to the close method in the messages controller. For more information about the form_for helper method read here


Do you have an update method in your controller, if not you can just do this:

<%= form_for( @message ) do |f|
  <p>Are you sure you wish to close this message?</p>
  <%= hidden_field_tag :close %>
  <%= f.submit "Close Message" %>
<% end %>

Then in your controller:

class MessagesController < ApplicationController

  def update
    @message = Message.find(params[:id])
    @message.completeDate = Date.today if params.has_key?(:close)
    if @message.save
      redirect_to myHomeMessages_path,
        :flash => { :success => "Your message was closed." }
    else
       redirect_to myHomeMessages_path, 
         :flash => { :error => "Error closing message." }
    end
  end
end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜