您看到的原因AbstractController::ActionNotFound是因为action genlist不存在于当前控制器中,但存在于TracksController. 您可以在button_to链接中指定应该解决此问题的控制器:
<%= button_to('Generate Playlist', :action => 'genlist', :controller => 'tracks' , :method => :get, :remote => true) %>
并config/routes.rb确保您拥有以下内容:
resources :tracks do
collection do
get 'genlist'
end
end
更新:
button_tohelper 不支持任何method期望使用的参数。该method参数被附加到生成的 URI 查询字符串中,例如“/tracks/genlist?method=post”,并在帮助程序生成 action的中使用。formbutton_to
以下是button_tohelper 的使用及其生成的内容(从输出中删除了authentity_token 隐藏字段):
<%= button_to('Generate Playlist', :action => 'genlist', :controller => 'tracks' , :method => :get, :remote => true) %>
# <form method="post" class="button_to" action="/tracks/genlist?method=get&remote=true">
# <div>
# <input type="submit" value="Generate Playlist">
# </div>
# </form>
虽然文档:http ://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-button_to声明:post, :get, :delete, :patch, and :put方法受支持,但没有明确说明这些方法会发生什么。
因此,在这种情况下,不要对请求使用 helper,而是使用button_tohelper ,如下所示:getlink_to
<%= link_to('Generate Playlist', :action => 'genlist', :controller => 'tracks' , :method => :get, :remote => true) %>
这将产生:
<a data-remote="true" href="/tracks/genlist?method=get">Generate Playlist</a>