Rails: use button_to for create action of controller

白昼怎懂夜的黑 提交于 2019-12-12 04:59:37

问题


Why can I not get my button_to to specify the create action of my controller rather than show

I have tried numerous times to add :action => "create" and other such things to the button_to parameters

<%= button_to "subscribe", subscription_path(feed_url: @feed.feed_url)%>

do I need to specify a create route in my routes.rb? if so how should I go about that?

When used the way that line is, I get this error on load:

Routing Error

No route matches {:action=>"show", :controller=>"subscriptions", :feed_url=>"http://foo.com/rss"}

in routes.rb I have this as the only reference to subscriptions.

  resource :subscriptions

回答1:


You need to use subscriptons_path, not subscription_path.

subscription_path is for showing a specific subscription.
subscriptions_path is for showing all subscriptions (via a GET request) or creating new subscriptions (via a POST request).

<%= button_to "subscribe", subscriptions_path(feed_url: @feed.feed_url, :method => :post)%>


来源:https://stackoverflow.com/questions/10488778/rails-use-button-to-for-create-action-of-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!