Rails搜索表可点击链接

时间:2016-08-03 13:13:22

标签: ruby-on-rails ruby search hyperlink

我有一个关键字搜索页面,其结果显示在表格中。此表中的一个字段是一个url,当字段中包含一个字段时,我希望将其显示为可点击链接但是我无法使其工作,当单击链接时,它会再次查询搜索。我认为问题是由于搜索控制器中的def处理关键字搜索视图。有人可以帮忙吗?

以下是包含结果表的关键字搜索视图:

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

所有这些链接都是可点击的,但不是将我带到正确的网址,而是重新搜索。

1 个答案:

答案 0 :(得分:0)

尝试此操作而不是硬编码<a> tag

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

您还可以发布网址样本吗?请务必包含协议(例如http://..。)