Ich habe views/search/search_textbook.html.erb
, die die Ergebnisse der Suche zeigt. Wenn ein Benutzer auf textbook.title von <%=link_to "#{textbook.title}", textbook_path(textbook.id) %>
klickt, ruft der Benutzer die show
-Methode von textbooks_controller
auf.Wie kann ich "link_to 'Zurück zum Suchergebnis'" Feature hinzufügen Rails
jedoch innerhalb views/textbooks/show.html.erb
hat <%= link_to 'Back to list', textbooks_path %>
, dass nur Benutzer auf die Indexseite des Lehrbuches übernehmen (views/textbooks/index.html.erb
).
unten ist meine Seite, die die Suche views/search/search_textbook.html.erb
Ergebnisse zeigt:
<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">✓</p>
<% else %>
<!--<%= "\u2715" %>-->
<p class="offer_no">✕</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>
unten ist mein views/textbooks/show.html.erb
Datei:
<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>
Wie kann ich eine clickalbe Taste back to search results
IF ONLY hinzufügen, wenn der Benutzer gekommen ist aus den Suchergebnissen ??
Danke!
Das ist brilliant! ! Vielen Dank! Aber meine Suche Aktion ist nicht in Lehrbuch-Controller. Die 'search' Aktion ist in' search_controller', sollte ich ersetzen? –
Es funktioniert! Aber ich möchte die Flash-Nachricht ausblenden. –
Glücklich, dass für Sie gearbeitet hat! Es spielt keine Rolle, wo Ihre Aktion ist, wenn Sie zur nächsten Aktion gelangen, die Sie auf das Flash-Objekt zugreifen können. Die Flash-Nachricht ist normalerweise nur eine Nachricht, die den Wert eines Flash anzeigt, wenn es existiert. flash [: notice] macht das selbe wie flash [: last_search_path], der einzige Unterschied ist, dass es eine Nachricht anzeigt, überprüfen Sie Ihre Anwendungslayout-Ansichtsdatei und sehen Sie, welche Bedingungen Sie haben. – bTazi