导轨,方法,我需要帮助
我需要编写一个方法来返回布尔值是否设置为 true/false。
就我而言,布尔值是产品的属性。为了跳过结帐过程中的一个步骤,我需要让这个 self 方法的工作类似于以下内容:
def current_user
return @current_user if defined?(@current_user)
@current_user = current_user_session && current_user_session.user
end
这个示例来自 application_controller,并且在结帐过程中执行与我需要在此处执行的操作相同的操作。
一旦我让它工作,它的用途是:
def checkout_steps
checkout_steps = %w{registration billing shipping shipping_method payment confirmation}
checkout_steps.delete "registration" if current_user
checkout_steps
end
我需要一个与上面的删除项工作方式相同的布尔值。我正在尝试了解这是如何工作的,因此非常感谢任何解释。
想法?
I need to write a method that returns whether or not a Boolean is set to true/false.
In my case the boolean is an attribute for a product. And to skip a step in the checkout process I need to have this self method work similar to the following:
def current_user
return @current_user if defined?(@current_user)
@current_user = current_user_session && current_user_session.user
end
this example is from the application_controller, and is doing the same thing in the checkout process that I need to do here.
The use for this once I have it working is:
def checkout_steps
checkout_steps = %w{registration billing shipping shipping_method payment confirmation}
checkout_steps.delete "registration" if current_user
checkout_steps
end
I need a boolean that works the same as the delete item above. I am trying to learn how this works so any explanation is greatly appreciated to.
thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不太确定我是否明白你的问题,但如果你问这一行是如何工作的:
这不是
current_user
返回其值的方式。它是 Ruby 的一种内置语法,您可以按以下形式指定一行 if 语句:而不是更传统的方式:
与完整的 if/end 语法一样,Ruby 将仅在条件为 true 时评估该语句,并且该条件可以是您通常放置在 if 条件中的任何代码。
在您的情况下,如果产品的属性类似于
can_be_shipped
,指示该商品是否可以发货,您可以简单地执行Rails 支持另一种语法来测试条件是否评估为 false
:逻辑更清晰:
I'm not quite sure I follow your question, but if you're asking how this line works:
It's not the way
current_user
returns its value that's doing it. It's a built-in syntax of Ruby where you can specify one line if statements in the following form:instead of the more traditional way:
As will the full if/end syntax, Ruby will only evaluate the statement if the condition is true, and the condition can be any code you might normally place in an if condition.
In your case, if the product's attribute is something like
can_be_shipped
, indicating whether the item can be shipped you can simply doRails supports another syntax for testing whether the condition evaluates to false:
Which could be used to make the logic clearer:
试试这个:
然后我将你的 check_steps 重写为:
我希望这有帮助!
Try this:
Then I'd rewrite your check_steps to be:
I hope this helps!