我想将参数传递recipient
给控制器。我可以像这样在我的视图中显示它:
@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 %>