2

这是我的第一个问题,所以我希望我可以尽可能具体,但不要苛刻。

我正在使用 Rails 进行敏捷 Web 开发,并且对编程非常陌生。

当我在订单/新页面上时,我想隐藏一个“结帐”按钮,这样它就不会对用户购买做任何讨厌的事情。

目前,我并不真正了解实例变量是如何工作的,因为似乎无论我在哪里声明我的实例变量,在视图中或在 orderscontroller#new 中,它总是验证为真。

这似乎是因为当我在视图中使用实例变量来隐藏 div 时(使用 hidden_​​div_if(condition == true) )按钮总是被隐藏!

不仅如此,当我这样做时:

<%= hidden_div_if( @hide_checkout_button == false ) do %>
<td><%= button_to 'Empty cart', cart, :method => :delete,
                  :confirm => 'Are you sure?' %></td>
<% end %>
<%= hidden_div_if( @hide_checkout_button == true ) do %>
  <td><%= button_to "Checkout", new_order_path, :method => :get %></td>
<% end %>

两个按钮都被隐藏了!这个怎么可能!?!

在本例中,我将变量声明放在了 views\orders_form.html.erb 中:

<%= @hide_checkout_button = true %>
<%= form_for(@order) do |f| %>
  <% if @order.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@order.errors.count, "error") %> 
        prohibited this order from being saved:</h2>

      <ul>
        <% @order.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    ...

这是一个讨论论坛,其中包含此问题的解决方案,但它们对我不起作用,我不知道为什么: http: //pragprog.com/wikis/wiki/Pt-G-2/version/35

这是问题本身:

如果在结帐屏幕已显示时单击侧边栏中的结帐按钮会发生什么?你能找到在这种情况下禁用按钮的方法吗?(提示:控制器中设置的变量在布局和局部以及直接呈现的模板中可用。)

如果您需要更多信息来提供帮助,请询问,我不太确定要提供多少细节或哪些信息很重要。

提前致谢 :-)

def new
    @cart = current_cart
    if @cart.line_items.empty?
      redirect_to store_url, :notice => "Your cart is empty"
      return
    end

    puts 34
    @hide_checkout_button = true
    @order = Order.new
    puts 37

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @order }
    end
  end

在 2012-11-13 20:33:40 +0000 开始获取 127.0.0.1 的“/assets/logo.png” 服务资产 /logo.png - 304 未修改(1 毫秒)[2012-11-13 20:33: 40] WARN 无法确定响应正文的内容长度。设置响应的内容长度或设置 Response#chunked = true * 34 37 * /orders/new

2012-11-13 20:33:49 +0000 OrdersController#new as HTML ←[1m←[36mCart Load (1.0ms)←[0m ←[1mSELECT”开始为 127.0.0.1 获取“/orders/new”购物车".* FROM "购物车" WHERE "购物车"."id" = ? LIMIT 1←[0m [["id", 63]] ←[1m←[35m (1.0ms)←[0m SELECT COUNT(*) FROM "line_items" WHERE "line_items"."c art_id" = 63

4

2 回答 2

1

问题是缺少=. <%= %>改为做。

<%= hidden_div_if( @hide_checkout_button == true ) do %>
  <td><%= button_to "Checkout", new_order_path, :method => :get %></td>
<% end %>

此外,对于布尔检查,您可以这样做if(boolean),其评估结果与您输入== true/false

<%= hidden_div_if( @hide_checkout_button) do %>
   #...
<% end %>

您想在调用视图的方法上设置该变量。在这种情况下,它newOrdersController

def new
  @hide_checkout_button = true
end

编辑:

将此添加到隐藏的 div 中,以查看它是否有助于找到将属性设置为的元素

<%= hidden_div_if(@hide_checkout_button, id: 'cart') do %>

with(在应用程序助手中)

def hidden_div_if(condition, attributes = {}, &block 
  if condition
    attributes["style"] = "display: none" 
  end
  content_tag("div", attributes, &block)
end

如果那不这样做,那么就这样做

<% if @hide_checkout_button %>
  <td><%= button_to "Checkout", new_order_path, :method => :get %></td>
<% else %>
  <td><%= button_to 'Empty cart', cart, :method => :delete, :confirm => 'Are you sure?' %></td>
<% end %>
于 2012-11-13T18:50:30.567 回答
0

用过的

<div class="actions">
<% if not @hide_checkout_button %>
<%= button_to 'Checkout', new_order_path, method: :get, class: "checkout" %>
<% end %>
<%= button_to 'Empty cart', cart, method: :delete, data: { confirm: "Are you sure?" }, remote: true %>
于 2021-12-12T17:54:38.093 回答