Seong Kim Seong Kim - 7 months ago 8
HTML Question

How can I add "link_to 'Back to search result'" feature Rails

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!

Answer

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

Comments