Ruby on rails 如何将隐藏参数传递给Rails3中的控制器

Ruby on rails 如何将隐藏参数传递给Rails3中的控制器,ruby-on-rails,ruby-on-rails-3,view,parameter-passing,Ruby On Rails,Ruby On Rails 3,View,Parameter Passing,我想将参数recipient传递给控制器。我可以这样在我的视图中显示它: @recipient.username 如何将此值传递给参数[:message][:recipient]?请注意,我没有名为“Message”的模型 控制器/消息\u controller.rb def deliver recipient = User.find_by_username(params[:recipient]) subject = params[:subject] body = params[:

我想将参数
recipient
传递给控制器。我可以这样在我的视图中显示它:

@recipient.username
如何将此值传递给
参数[:message][:recipient]
?请注意,我没有名为“Message”的模型

控制器/消息\u 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 %>

:messages,:action=>:deliver)do | f |%>



您可以通过为收件人分配一个属性可访问密钥并将其分配给表单来完成此操作

form_for :message do |f|
  f.hidden_field :recipient_id, :value => @recipient.id.to_i
  f.text_field :message
  f.submit
end
将此消息传递给创建操作后,您就可以检查参数[:message][:recipient\u id]

然后把这个传给数据库

玩得开心

看看这个