我正在使用Rails 4.我尝试将button_to组成一个类:
<%= button_to "unsubscribe",
:form_class => 'unsubscribe',
:controller => :places,
:action => 'delid',
:plid => subscription.place.id,
:method => :post %>
但表单类是“button_to”
控制器看起来像这样:
def delid
@lolka = Subscription.where(user_id: current_user.id, place_id: params[:plid]).destroy_all
@subscribed = Place.find_by_id(params[:plid])
@subscribed.update_attributes(:subscribers_count => Subscription.where(place_id: params[:plid]).count)
@subscribed.update_attributes(:subscribers_male => Subscription.where(place_id: params[:plid], user_gender: 'male').count)
@subscribed.update_attributes(:subscribers_female => Subscription.where(place_id: params[:plid], user_gender: 'female').count)
@places = Place.all
redirect_to :back
end
也许我做错了什么?并且最好使用link_to?但怎么样?提前谢谢。
答案 0 :(得分:1)
阅读documentation; :form_class
(以及method
)位于第二个 html_options
哈希中:
<%= button_to "unsubscribe", {
controller: :places,
action: 'delid',
plid: subscription.place.id,
}, {
method: :post
form_class: 'unsubscribe'
} %>