简体   繁体   English

如何重构条件语句?

[英]How do I refactor a conditional statement?

I'm on Rails 4 and Im using devise for user management. 我在Rails 4和Im上使用devise进行用户管理。 I have a simple conditional statement in a show view like this 我在这样的显示视图中有一个简单的条件语句

<% if user_signed_in? %>
    <%= link_to "Checkout", user_cart_path(current_user,@cart), method: :patch %>
<% else %>
    <%= link_to "Checkout", signin_path %>
<% end %>

I want to clean up my view and add make the conditional a method. 我想清理我的视图并添加使该条件成为方法。

I have tried adding the method 我尝试添加方法

def checkout_link(current_user)
    if user_signed_in?
        link_to "Checkout", user_cart_path(current_user, self), method: :patch
    else 
        link_to "Checkout", signin_path
    end
end

to my cart model, and replaced the conditional in my show cart view to 到我的购物车模型,并替换了我的显示购物车视图中的条件

<%= @cart.checkout_link(current_user) %>

When I load the cart show page, I get an error 加载购物车显示页面时,出现错误

undefined method `user_signed_in?' for #<Cart:0x007fd015525228>

What am I doing wrong? 我究竟做错了什么? Thanks. 谢谢。

The thing you are doing wrong is concerning your model with the view. 您做错的事情是与视图有关的模型。 This should not be done. 不应这样做。

You should place the checkout_link in a view helper method. 您应该将checkout_link放在视图帮助器方法中。 This will clean up your view, and keep the model only concerned about the data it contains. 这将清理您的视图,并使模型仅关注其中包含的数据。

Though, in this case, I would just have a single link that leads to the checkout, and if the user is not signed in, have a before_filter redirect them to sign in. 不过,在这种情况下,我只有一个链接可导致结帐,并且如果用户未登录,请使用before_filter重定向他们以登录。

You have two options, you can either add to the view helper or use a decorator. 您有两个选择,可以添加到视图助手或使用装饰器。

  1. If you add the method to the view, it would look something like this: 如果将方法添加到视图中,它将看起来像这样:

      def checkout_link(cart) if user_signed_in? link_to "Checkout", user_cart_path(current_user,cart), method: :patch else link_to "Checkout", signin_path end end 
  2. "The decorator wraps the model, and deals only with presentational concerns. In the controller, you decorate the article before handing it off to the view." “装饰器包装了模型,仅处理表示方面的问题。在控制器中,您在将文章交给视图之前先对其进行了装饰。” 1 If you would like to use a decorator, check out Ryan Bates' Railscast on them: using an existing gem or creating a decorator from scratch . 1如果您想使用装饰器,请查看Ryan Bates的Railscast: 使用现有的gem从头开始创建装饰器

1 Quoted from the Draper gem's README. 1引自Draper gem的README。 https://github.com/drapergem/draper https://github.com/drapergem/draper

You're almost there. 你快到 You want your checkout_link method to be a "helper method" in a helper file, not in your model file. 您希望您的checkout_link方法在帮助程序文件中而不是模型文件中成为“帮助程序方法”。 So, create a file named app/helpers/carts_helper.rb and move your checkout_link method there. 因此,创建一个名为app/helpers/carts_helper.rb的文件,然后在其中移动您的checkout_link方法。 Methods in helper files are available to views and controllers. 帮助文件中的方法可用于视图和控制器。

When I was just learning my way around Rails, I would do: 当我刚开始学习Rails时,我会做:

rails generate scaffold Dummy

specifically to see what files it generated, including helper files. 专门查看它生成了哪些文件,包括帮助文件。 This might be a good place to start. 这可能是一个不错的起点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM