我想将参数recipient
传递给控制器。我可以在我看来这样显示:
@recipient.username
如何将此值传递给params[:message][:recipient]
?请注意,我没有名为“Message”的模型。
控制器/ 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 %>
答案 0 :(得分:2)
您可以通过为收件人分配attr_accessible密钥并将其分配给表单来完成此操作。
form_for :message do |f|
f.hidden_field :recipient_id, :value => @recipient.id.to_i
f.text_field :message
f.submit
end
将此传递给您的创建操作后,您可以检查参数[:message] [:recipient_id]
并将其传递给db。
玩得开心