0

I'm working on an application that is basically an online store using Ruby on Rails. I have application.html.erb which holds a side with the basket that runs with Ajax. Once the checkout button is clicked it redirects the body of the page for the a new order form. I want the checkout button to desapear when that view is displayed. Any thoughts on how I could do it? Follows the code for the partial

views/baskets/_basket.html

    <% unless basket.queue_groceries.empty? %>

<div class="basket"> Our basket</div>
  <table>
   <!-- START_HIGHLIGHT -->
   <%= render(basket.queue_groceries) %>

   <!-- END_HIGHLIGHT -->

     <tr class="total_line">
       <td colspan="2">Total</td>
        <!-- START_HIGHLIGHT -->
       <td class="total_cell"><%= number_to_currency(basket.total_price) %></td>
        <!-- END_HIGHLIGHT -->
     </tr>

   </table>

   <!-- START_HIGHLIGHT -->
  <%= button_to 'Check out', new_order_path, method: :get %>
  <%= button_to 'Empty basket', basket, method: :delete, confirm: 'Are you sure?'  %>

And then the views/layouts/application.html.erb

<!-- START:head -->
<!DOCTYPE html>
<html>
<head>
  <!-- START_HIGHLIGHT -->
  <title>Pragprog Books Online Store</title>
  <!-- END_HIGHLIGHT -->
  <!-- <label id="code.slt"/> --><%= stylesheet_link_tag    "application", media: "all",   "data-turbolinks-track" => true %>

  <%= javascript_include_tag "application", "data-turbolinks-track" => true %><!-- <label  id="code.jlt"/> -->
  <%= csrf_meta_tags %><!-- <label id="code.csrf"/> -->
</head>
<!-- END:head -->
<body class="<%= controller.controller_name %>">
<!-- START_HIGHLIGHT -->
<div id="banner">
  <%= image_tag("logo.png") %>
  <%= @page_title || "Pragmatic Bookshelf" %><!-- <label id="code.depot.e.title"/> -->
</div>  
<div id="columns">
  <div id="side">
    <div id="basket">

  <%= render @basket %>

    </div>
    <ul>
      <li><a href="http://www....">Home</a></li>
      <li><a href="http://www..../faq">Questions</a></li>
      <li><a href="http://www..../news">News</a></li>
      <li><a href="http://www..../contact">Contact</a></li>
    </ul>
  </div>
  <div id="main">
    <!-- END_HIGHLIGHT -->
   <%= yield %><!-- <label id="code.depot.e.include"/> -->
    <!-- START_HIGHLIGHT --> 
  </div>
 </div>
<!-- END_HIGHLIGHT -->
</body> 
</html>
vinibol12
  • 468
  • 4
  • 21
  • I assume this is what you're looking for? [http://stackoverflow.com/questions/13395153/how-to-render-partial-on-everything-except-a-certain-action](http://stackoverflow.com/questions/13395153/how-to-render-partial-on-everything-except-a-certain-action) – Aventuris Jan 05 '15 at 22:17

1 Answers1

1

You can do it with following check:

<%= button_to 'Check out', new_order_path, method: :get     # create a button
    unless controller.controller_name == 'orders' &&        # unless controller is orders
           %w(new create).include?(controller.action_name)  # AND actions include new and create
%>

%w(new create) is an alias for ['new', 'create'] array. We check if it includes current action name. If yes - we won't show the button.

Why create action is included: When you get a failure in your create action in controller, you call render :new, which does not redirect you to new action, but simply renders that template. So the action is still create although we use other action's template.

Felix Borzik
  • 1,230
  • 12
  • 19
  • I did it. The button is still there when the error notice turns up though. Looking at your suggestion helped me to learn a bit about the use of those rails methods and some ruby syntax though. Thanks man. – vinibol12 Jan 08 '15 at 22:08
  • Updated answer for error messages rendering too. Now if `create` action renders error message and `new` template, it will be hidden, too. – Felix Borzik Jan 08 '15 at 23:38
  • Thanks. This time it worked great. Sorry my ignorance But could you break down this code and explain how the sintaxe works? I'm really struggling to understand since I'm quite new to Rails and programming in general. – vinibol12 Jan 11 '15 at 08:20