3

I have views/search/search_textbook.html.erb that shows the results of search. When a user click textbook.title by <%=link_to "#{textbook.title}", textbook_path(textbook.id) %>, The user is invoking the show method from textbooks_controller.

However, inside views/textbooks/show.html.erb has <%= link_to 'Back to list', textbooks_path %> that only take user to the index page of textbook (views/textbooks/index.html.erb).

below is my page that shows the search results views/search/search_textbook.html.erb:

<div class="container">
   <div class="row">
      <div class="col-sm-12">        
         <table class = "table table-striped">
            <h1>Textbook Search Results</h1>

            <thead>
               <tr>
                  <th>Textbook Title</th>
                  <th>Subject</th>
                  <th>Price</th>
                  <th>Accept Offer</th>
                  <th>Created on</th>
               </tr>
            </thead>

          <tbody>
            <% @textbooks.each do |textbook| %>
              <tr>
                <!--<td><%= textbook.title %></td>-->
                <td><%=link_to "#{textbook.title}", textbook_path(textbook.id) %></td>
                <td><%= textbook.subject %></td>
                <td>$<%= textbook.price %></td>
                <td>
                  <% if textbook.offer == true %>
                    <!--<%= "\u2713"%>-->
                    <p class="offer_yes">&#x2713;</p>
                  <% else %>
                    <!--<%= "\u2715" %>-->
                    <p class="offer_no">&#x2715;</p>
                  <%end%>
                </td><!--somehow below need Date.parse()-->
                <td><%= textbook.created_at.strftime("%d %b. %Y") %></td>
              </tr>
            <% end %>
          </tbody>
        </table>

        <br>

        <div>
          <%= form_tag search_textbook_path, :method => :get do %>
            <p>
            <a style="color:#000000" title="Suggest!" data-toggle="popover" data-trigger="hover" data-content="Title or Subject">
              <%= text_field_tag :search, params[:search], placeholder: "Search textbooks" %>
            </a>
            <%= submit_tag "Search", :name => nil, class: "btn btn-success btn-sm" %>
          <% end %>
        </div>

        <%= link_to 'Sell Textbook', new_textbook_path, class: "btn btn-primary" %>
        <%= link_to 'Back to list', textbooks_path %>

        <script>
        $(document).ready(function(){
            $('[data-toggle="popover"]').popover();   
        });
        </script>

    </div>
  </div>
</div>

below is my views/textbooks/show.html.erb file:

<div class="container">
    <div class="row">
        <!--<div class="col-xs-offset-4 col-xs-4 col-xs-offset-4">-->
        <div class="col-sm-offset-4 col-sm-4 col-sm-offset-4">

            <!--<p id="notice"><%= notice %></p>-->

            <p>
                <h4><strong>Title:</strong>
                    <%= @textbook.title %></h4>
                </p>

                <p>
                    <strong>Subject:</strong>
                    <%= @textbook.subject %>
                </p>

                <p>
                    <strong>Price:</strong>
                    $<%= @textbook.price %>
                </p>

                <p>
                    <strong>Accept Offer:</strong>
                    <%if @textbook.offer == true%>
                    <%='Yes'%>
                    <%else%>
                    <%='No'%>
                    <%end%>
                </p>

                <p>
                    <strong>Description:</strong>
                    <pre><%= @textbook.description %></pre>
                </p>

                <p>
                    <% if !(@textbook.thumbnail_content_type =~ /^image/).nil? %>
                    <strong>Image:</strong>
                    <pre><a href="<%= @textbook.thumbnail.url %>"><%= image_tag @textbook.thumbnail.url(:medium) %></a></pre>
                    <%else%>
                    <!--<strong>No Image.</strong>-->
                    <%end%>
                </p>

                <p>
                    <strong>Created on:</strong>
                    <%= @textbook.created_at.strftime("%d %b. %Y") %>
                </p>

                <p>
                    <!--<%= link_to 'Contact', new_contact_path %>-->
                    <%= link_to 'Contact', new_contact_textbook_path(@textbook), class: 'btn btn-info' %>

                </p>

                <%if @textbook.user_email == current_user.email %>
                <%= link_to 'Edit', edit_textbook_path(@textbook) %> |
                <%= link_to 'Back to list', textbooks_path %>
                <%else %>
                <%= link_to 'Back to list', textbooks_path %>
                <%end%>
        </div>
    </div>
</div>

How can I add a clickalbe button back to search results IF ONLY IF the user is came from search results??

Thanks!

Seong Kim
  • 535
  • 5
  • 22

2 Answers2

1

This is how I would have done it.

1) Find the current path and assign it to a flash:

flashes are not only used for messages, they are actually the simplest way to send temporary data to an other action. So in the search action of the textbook controller:

flash[:last_search_path] = [the current path]

note: [the current path] isn't ruby code, we'll see later how to find it

2) Use the link in your textbook show view:

In textbook show view all you have to do is:

<% if flash[:last_search_path] %>
  <%= link_to "back to search results", flash[:last_search_path] %>
<% end %>

But... how do you find [the current path] ? Obviously [the current path] isn't ruby code, so you'll have to replace it with something else. there's multiple ways to find the current path or url: look here and here

Flashes ?

Flashes can be very useful for passing temporary data, I highly suggest you to read more about them. Learn more about flashes here

Other solutions to pass data between actions

You can use params to pass that link parameters but it would add more complexity. read more about params here

You can also use cookies but that would save it to the users browser and wouldn't work only if he came from the search result: read more about sessions here

Community
  • 1
  • 1
Badr Tazi
  • 749
  • 1
  • 6
  • 20
  • This is brilliant ! ! Thank you! But my search action is not in textbook controller. The `search` action is in `search_controller` should I replace? – Seong Kim Apr 23 '16 at 19:23
  • 1
    It works! But I would like to hide the flash message. – Seong Kim Apr 23 '16 at 20:48
  • Happy that worked for you! It doesn't matter where your action is when you get to the next action you'll be able to access the flash object. The flash message is usually just a message that displays the value of a flash when it exists. flash[:notice] does the exact same thing as flash[:last_search_path], the only difference is that it displays a message, check your application layout view file and see what conditionals you have. – Badr Tazi Apr 24 '16 at 04:46
  • I found flash is good, but I found better answer and tried. See below! Thanks anyway. – Seong Kim Apr 24 '16 at 04:56
1

Above flash is good way. But I use below way.

<p>
    <%if URI(request.referer).path == "/search_textbook" %>
       <%= link_to 'Back to Search Result', request.referer, class: "btn btn-warning" %>
    <%end%>
</p>
Seong Kim
  • 535
  • 5
  • 22