我正在尝试向另一个用户发送消息。但是当我将一个字段留空时,控制器会呈现另一个“新”动作。
这是链接到第一个提交的视图代码:
<%= link_to 'Send Mail', {:controller => 'messages', :action => 'new', :recipient_id => @profile.id}, :class => 'btn btn-large btn-primary' %>
这是消息#new 代码:
def new
@message = Message.new
@recipient = Profile.find(params[:recipient_id])
end
这是消息#new 查看代码:
<%= simple_form_for @message do |f| %>
<%= f.input :title %>
<%= f.input :body %>
<%= f.input :recipient_id, :as => :hidden, :input_html => {:value => @recipient.id} %>
<%= f.input :sender_id, :as => :hidden, :input_html => {:value => @recipient.id} %>
<%= f.button :submit %>
<% end %>
这是消息#create 代码:
def create
@message = Message.new(params[:message])
if @message.save
flash[:notice] = "New message created"
redirect_to @message
else
flash[:error] = "Did not work"
@recipient = params[:recipient_id]
render action: "new"
end
end
如何获取表单中提交的 :recipient_id 值并传递给渲染新操作?