导轨,方法,我需要帮助

发布于 2024-08-23 18:22:38 字数 701 浏览 0 评论 0原文

我需要编写一个方法来返回布尔值是否设置为 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 技术交流群。

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

发布评论

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

评论(2

时光瘦了 2024-08-30 18:22:38

我不太确定我是否明白你的问题,但如果你问这一行是如何工作的:

checkout_steps.delete "registration" if current_user

这不是 current_user 返回其值的方式。它是 Ruby 的一种内置语法,您可以按以下形式指定一行 if 语句:

<statment> if <condition> # only execute <statement> if <condition> is true

而不是更传统的方式:

if <condition> then
  <statement>
end

与完整的 if/end 语法一样,Ruby 将仅在条件为 true 时评估该语句,并且该条件可以是您通常放置在 if 条件中的任何代码。

在您的情况下,如果产品的属性类似于 can_be_shipped ,指示该商品是否可以发货,您可以简单地执行

checkout_steps.delete 'shipping' if [email protected]_be_shipped

Rails 支持另一种语法来测试条件是否评估为 false

<statment> unless <condition> # only execute <statement> if <condition> is false

:逻辑更清晰:

checkout_steps.delete 'shipping' unless @product.can_be_shipped

I'm not quite sure I follow your question, but if you're asking how this line works:

checkout_steps.delete "registration" if current_user

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:

<statment> if <condition> # only execute <statement> if <condition> is true

instead of the more traditional way:

if <condition> then
  <statement>
end

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 do

checkout_steps.delete 'shipping' if [email protected]_be_shipped

Rails supports another syntax for testing whether the condition evaluates to false:

<statment> unless <condition> # only execute <statement> if <condition> is false

Which could be used to make the logic clearer:

checkout_steps.delete 'shipping' unless @product.can_be_shipped
り繁华旳梦境 2024-08-30 18:22:38

试试这个:

def current_user
  current_user_session.user if current_user_session
end

def signed_in?
  current_user_session && !current_user_session.user.nil?
end

然后我将你的 check_steps 重写为:

checkout_steps.delete "registration" if signed_in?

我希望这有帮助!

Try this:

def current_user
  current_user_session.user if current_user_session
end

def signed_in?
  current_user_session && !current_user_session.user.nil?
end

Then I'd rewrite your check_steps to be:

checkout_steps.delete "registration" if signed_in?

I hope this helps!

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