使用 link_to 切换布尔值

发布于 2024-12-06 14:10:25 字数 1982 浏览 1 评论 0原文

我已经阅读了相关的堆栈问题,但似乎仍然使用以下代码遇到路由错误:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谁把谁当真 2024-12-13 14:10:25

使用您的路线的链接将是:

link_to("Toggle On", membership_toggleon_url(membership), :method => :put, :title => "This toggles it on")
link_to("Toggle Off", membership_toggleoff_url(membership), :method => :put, :title => "This toggles it off")

您需要提供 :method =>; :put 到 link_to 方法,因为这就是您的路由文件中的路由。

这样的事情可以通过单个操作来实现:

resources :memberships do
  member do
    put :toggle
  end
end

然后您可以通过使用 Rails 切换方法。

这会让你做类似的事情:

@membership.toggle(:on)

Links using your routes are going to be:

link_to("Toggle On", membership_toggleon_url(membership), :method => :put, :title => "This toggles it on")
link_to("Toggle Off", membership_toggleoff_url(membership), :method => :put, :title => "This toggles it off")

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:

resources :memberships do
  member do
    put :toggle
  end
end

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:

@membership.toggle(:on)
半窗疏影 2024-12-13 14:10:25

尝试将路线设置为 :on =>; :成员

此外,还有一个原子 toggle! 方法,其功能正如其名称所暗示的那样。

Try setting the route as being :on => :member.

Also, there's an atomic toggle! method that does what its name implies.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文