1

我认为我有一个代码

<% topics.each do |t| %>
<li><%= link_to "#{t.name}", {:controller => "pages", :action => "session_start", :topic_id => t.id} , :method => :post , :confirm => "Start a new QA session for '#{t.name}' ?"  %></li>
<% end %>

参数 :topic_id 作为 get 发送并附加到 url,我希望它作为帖子正文的一部分发送。我尝试了各种组合,甚至阅读了 link_to API 文档。

我错过了什么?

4

3 回答 3

2

actually, you can do a rake routes to check the routes which you want to POST. your routes should have something like this:

  resources :pages do
    member do
      post   'session_start'
    end
  end

then in your link, you can just do

<%= link_to "Hello", session_start_pages_path() %>
于 2013-04-03T14:43:04.230 回答
2

为什么不做类似的事情:

<%= link_to "#{t.name}", pages_session_start_path(topic_id: t.id), method: :post, confirm: "Start a new QA session for '#{t.name}' ?" %>

PS 从我的头顶上掉下来,我不记得它是否会,pages_session_start_path或者session_start_pages_path但无论哪种方式,您都可能希望将您的 routes.rb 中的 URL 帮助程序重命名为更令人难忘的东西。

确保此 URL 的路由设置为POST

post "/pages/session_start", to: "pages#session_start", as: :session_start
于 2013-04-03T14:37:03.317 回答
-1

查看link_to文档,有一个url_optionshtml_options哈希;该选项method必须属于url_options散列,但是当您编写代码时,它属于html_options散列。尝试将所有选项移动到一个哈希中(在您的示例中,您不需要html_options哈希):

<%= link_to "#{t.name}", {:controller => "pages", :action => "session_start", :topic_id => t.id, :method => :post , :confirm => "Start a new QA session for '#{t.name}' ?" } %>
于 2013-04-03T14:31:23.460 回答