将表单指向rails上的特定控制器操作ruby

时间:2013-10-10 21:34:02

标签: ruby-on-rails ruby ruby-on-rails-3.2

有谁知道如何将以下评论表格指向我的评论控制器中的hello操作,任何建议都将不胜感激:

<h2>Add a comment:</h2>
<%= form_for([@venue, @venue.comments.build]) do |f| %>
  <p>
    <%= f.label :commenter %><br />
    <%= f.text_field :commenter %>
  </p>
  <p>
    <%= f.label :body %><br />
    <%= f.text_area :body %>
  </p>
  <p>
    <%= f.submit %>
  </p>
<% end %>

//////////////////我的评论员:

def hello
     @venue = Venue.find(params[:venue_id])
     @comment = @venue.comments.create(params[:comment].permit(:commenter, :body))
     redirect_to venue_path(@venue)
  end

1 个答案:

答案 0 :(得分:1)

您必须正确设置路线,例如:

resources :venues do
  resources :comments do
    collection do
      post :hello
    end
  end
end

并将表单网址设置为此操作的网址:

 <%= form_for(@venue.comments.build, url: hello_venue_comments_path(@venue)) do |f| %>