2016-04-23 13 views
3

私はviews/search/search_textbook.html.erbに検索結果を表示しています。ユーザがtextbook.titleを<%=link_to "#{textbook.title}", textbook_path(textbook.id) %>でクリックすると、ユーザはtextbooks_controllerからshowメソッドを呼び出しています。"link_to"をどのように追加することができますか? "検索結果に戻る"機能

ただし、views/textbooks/show.html.erbの中には<%= link_to 'Back to list', textbooks_path %>があり、ユーザーはテキストブック(views/textbooks/index.html.erb)のインデックスページにしか行きません。利用者が来ている場合だけ、私はclickalbeボタンback to search resultsを追加することができますどのように

<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> 

以下
<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> 

は私views/textbooks/show.html.erbファイルである:以下

は、検索が views/search/search_textbook.html.erb結果を示して、私のページです検索結果から

ありがとうございます!

答えて

1

これが私のやり方です。

1)は現在のパスを検索し、フラッシュに割り当て:

点滅はメッセージのみに使用されていない、彼らは実際に他のアクションを一時的にデータを送信するための最も簡単な方法です。 だから教科書コントローラの検索アクションで:

flash[:last_search_path] = [the current path] 

はノート:[the current path]がコードをルビーされていない、我々はそれ

2)あなたの教科書のショーにリンクを使用して見つける方法を後で見ますビュー:

教科書ショービューですべてを行う必要がある:

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

しかし、あなたはどのように[現在の経路]を見つけるのですか? 明らかに[現在のパス]はルビコードではないので、別のもので置き換える必要があります。現在のパスまたはURLを見つけるために複数の方法があります :herehere

点滅を見て?

点滅は、一時的なデータを渡すために非常に役立つことがあります。私は非常に詳細を読むことをお勧めします。あなたがそのリンクパラメータを渡すためのparamsを使用することができますが、それはより多くの複雑さを追加したアクション

の間でデータを渡すためにLearn more about flashes here

その他のソリューションを提供しています。 read more about params here

また、クッキーを使用することができますが、それは、ユーザーのブラウザに保存しますと、彼は検索結果から来た場合にのみ機能しません。フラッシュの上read more about sessions here

+0

これは素晴らしいです! !ありがとうございました!しかし、私の検索行動は教科書のコントローラーにはありません。 'search'アクションは' search_controller'にありますか? –

+1

それは動作します!しかし、私はフラッシュメッセージを隠したいと思います。 –

+0

うれしいことがあなたのために働いた!あなたがフラッシュオブジェクトにアクセスすることができる次のアクションに着いたときのあなたのアクションがどこであるかは関係ありません。フラッシュメッセージは通常、フラッシュの値を表示するメッセージです。 flash [:notice]はflash [:last_search_path]とまったく同じことですが、唯一の違いは、メッセージを表示し、アプリケーションのレイアウトビューファイルを確認し、あなたが持っている条件を確認することです。 – bTazi

1

が良い方法です。しかし、私は以下の方法で使用します。

<p> 
    <%if URI(request.referer).path == "/search_textbook" %> 
     <%= link_to 'Back to Search Result', request.referer, class: "btn btn-warning" %> 
    <%end%> 
</p> 
関連する問題