使用 link_to 切换布尔值
我已经阅读了相关的堆栈问题,但似乎仍然使用以下代码遇到路由错误:
Routes.rb
resources :memberships do
put :toggleon
put :toggleoff
end
Memberships_controller.rb
def toggleon
@membership = Membership.find(params[:id])
@membership.update_attributes(:active => true)
if user.id == membership_id
redirect_to root_path
else
redirect_to group
end
end
def toggleoff
@membership = Membership.find(params[:id])
@membership.update_attributes(:active => false)
if user.id == membership_id
redirect_to root_path
else
redirect_to group
end
end
Show.html.erb
<% if this_membership.active %>
<%= link_to 'Pause', this_membership, controller: :memberships, method: :toggleoff, style: 'color:#ccc' %>
<% else %>
<%= link_to 'Start', this_membership, controller: :memberships, method: :toggleon, style: 'color:green' %>
<% end %>
错误文本
Started POST "/memberships/13" for 127.0.0.1 at 2011-09-27 23:35:35 +0100
ActionController::RoutingError (No route matches "/memberships/13"):
我可以很好地销毁会员资格,但切换会员资格活动被证明很棘手。
提前致谢!
第二次尝试
<%= link_to("Pause", membership_toggle(@membership), :method => :put, :title => "This toggles it off") %>
<% else %>
<%= link_to("Toggle On", membership_toggle(@membership), :method => :put, :title => "This toggles it on") %>
,
def toggle
@membership = Membership.find(params[:id])
@membership.toggle!(active)
end
现在
resources :memberships do
member do
put :toggle
end
end
给了我这个错误......
undefined method `membership_toggle' for #<#<Class:0x00000102e69bc0>:0x00000102e66ec0>
I've read through the relevant Stack questions but still seem to be hitting a routing error with the following code:
Routes.rb
resources :memberships do
put :toggleon
put :toggleoff
end
Memberships_controller.rb
def toggleon
@membership = Membership.find(params[:id])
@membership.update_attributes(:active => true)
if user.id == membership_id
redirect_to root_path
else
redirect_to group
end
end
def toggleoff
@membership = Membership.find(params[:id])
@membership.update_attributes(:active => false)
if user.id == membership_id
redirect_to root_path
else
redirect_to group
end
end
Show.html.erb
<% if this_membership.active %>
<%= link_to 'Pause', this_membership, controller: :memberships, method: :toggleoff, style: 'color:#ccc' %>
<% else %>
<%= link_to 'Start', this_membership, controller: :memberships, method: :toggleon, style: 'color:green' %>
<% end %>
Error text
Started POST "/memberships/13" for 127.0.0.1 at 2011-09-27 23:35:35 +0100
ActionController::RoutingError (No route matches "/memberships/13"):
I can destroy memberships just fine but toggling the membership activity is proving tricky.
Thanks in advance!
Second attempt
<%= link_to("Pause", membership_toggle(@membership), :method => :put, :title => "This toggles it off") %>
<% else %>
<%= link_to("Toggle On", membership_toggle(@membership), :method => :put, :title => "This toggles it on") %>
and
def toggle
@membership = Membership.find(params[:id])
@membership.toggle!(active)
end
and
resources :memberships do
member do
put :toggle
end
end
now gives me this error...
undefined method `membership_toggle' for #<#<Class:0x00000102e69bc0>:0x00000102e66ec0>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用您的路线的链接将是:
您需要提供
:method =>; :put
到 link_to 方法,因为这就是您的路由文件中的路由。这样的事情可以通过单个操作来实现:
然后您可以通过使用 Rails 切换方法。
这会让你做类似的事情:
Links using your routes are going to be:
You need to supply
:method => :put
to the link_to method given that's what you have the route as in your routes file.Something like this can be achieved with a single action:
Then you can make use of a single boolean field in your controller by using the Rails toggle method.
This will let you do something like:
尝试将路线设置为
:on =>; :成员
。此外,还有一个原子
toggle!
方法,其功能正如其名称所暗示的那样。Try setting the route as being
:on => :member
.Also, there's an atomic
toggle!
method that does what its name implies.