0
我想將參數recipient
傳遞給控制器。如何將隱藏參數傳遞給Rails3中的控制器
@recipient.username
如何傳遞這個值params[:message][:recipient]
:我可以在我看來,這樣的表現呢?請注意,我沒有名爲「消息」的模型。
控制器/ messages_controller.rb
def deliver
recipient = User.find_by_username(params[:recipient])
subject = params[:subject]
body = params[:body]
current_user.send_message(recipient, body, subject)
redirect_to :controller => 'messages', :action => 'received'
flash[:notice] = "message sent!"
end
的意見/郵件/ new.html.erb
<td><%= @recipient.username if @recipient %></td>
<%=form_for :messages, url: url_for(:controller => :messages, :action => :deliver) do |f| %>
<div class="field">
<%= f.label :subject %><br />
<%= f.text_field :subject %>
</div>
<div class="field">
<%= f.label :body %><br />
<%= f.text_field :body %>
</div>
<div class="actions">
<%= f.submit %>
<% end %>
看一看這個http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-hidden_field – 2012-07-05 15:35:11