簡體   English   中英

Rails搜索表可點擊鏈接

[英]Rails search table clickable link

我有一個關鍵字搜索頁面,其結果顯示在表格中。 此表中的一個字段是一個url,當字段中包含一個字段時,我希望將其顯示為可點擊鏈接但是我無法使其工作,當單擊鏈接時,它會再次查詢搜索。 我認為問題是由於搜索控制器中的def處理關鍵字搜索視圖。 有人可以幫忙嗎?

以下是包含結果表的keywordsearch視圖:

<!-- Index of all Courses -->
<% provide(:title, "Courses Page") %>


<!--Breadcrumbs -->
<br>
<%= link_to "Back", :back %><br><br>

<!--Page Contents --> 


  <div class ="row">
    <h1>Degrees Offered</h1>
    <%= image_tag "line.png" , :alt => "line break"%>    
  </div>

  <div class ="row">

      <!-- Form for Keyword Search, to query database for University courses. It is hidden so as to not appear as a search on the page -->
      <div class = "hidden">         
      <%= form_tag(keywordsearch_path, :method => "get", id: "search-data") do %>
      <%= text_field_tag :search, params[:search], placeholder: "Search course" %>
      <%= submit_tag "Search" %>
      <% end %>
      </div>     
      <% if @search_degree != nil %>
      <% end %>

      <% if @search_degree != nil %>
        <table border="1" class="table">
          <thead>
            <tr>
              <th>University Name</th>
              <th>Course Name</th>
              <th>Duration</th>
              <th>Qualification</th>
              <th>Entry Requirements</th>
              <th>Course Page</th>
            </tr>
          </thead>
          <tbody>
            <% @search_degree.each do |degree| %>
            <tr>
              <td><%= degree.uname %></td>
              <td><%= degree.cname %></td>
              <td><%= degree.duration %></td>
              <td><%= degree.qualification %></td>
              <td><%= degree.entry %></td>
              <td> <a href=<% degree.url %>>View course page on University Website</a></td>
            </tr>
            <% end %>
          </tbody>
        </table>
      <% end %>
    </div>

以下是搜索控制器中定義關鍵字搜索的部分:

  def keywordsearch
    @search = Degree.all.select(:uname, :cname, :ucas, :duration, :qualification, :entry).distinct.order(id: :ASC)
    if params[:search]
      @search_degree = Degree.search(params[:search]).order('uname ASC')
    end
  end

所有這些鏈接都是可點擊的,但不是把我帶到正確的網址,它會重新搜索。

試試這個而不是硬編碼<a> tag

<%= link_to 'View course page on University Website', degree.url %>

你還可以發一個網址樣本嗎? 一定要包含協議(例如, http:// ..

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM