简体   繁体   English

将表单指向导轨上的特定控制器动作红宝石

[英]Point a form to a specific controller action ruby on rails

Does anyone know how to point the following comments form to the hello action in my comments controller below, any advice would be appreciated: 有谁知道如何将以下评论表单指向下面我的评论控制器中的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 %>

//////////////////my comments controller: /////////////////我的评论控制器:

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

You have to set route properly, for example: 您必须正确设置路由,例如:

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

and set form url to this action's url: 并将表单网址设置为该操作的网址:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM