Akamaru Akamaru - 2 months ago 6x
Ruby Question

Rails search table clickable link

I have a keyword search page for which the results are presented in a table. One of the fields in this table is a url which I want to display as a clickable link when the field has one in it but I can't get this working, when the link is clicked it instead queries the search again. I think the problem is due to the def within the searches controller dealing with the keywordsearch view. Can anybody help?

Below is the keywordsearch view which contains the results table:

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

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

<!--Page Contents -->

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

<div class ="row">

<!-- Form for Keyword Search, to Querry 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 %>
<% if @search_degree != nil %>
<% end %>

<% if @search_degree != nil %>
<table border="1" class="table">
<th>University Name</th>
<th>Course Name</th>
<th>Entry Requirements</th>
<th>Course Page</th>
<% @search_degree.each do |degree| %>
<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>
<% end %>
<% end %>

Below is portion of the search controller in which keyword search is defined:

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

The link from all of this is clickable but rather than taking me to the correct url it redoes the search.


try this instead of hardcoding the <a> tag:

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

Can you also post a sample of url? Be sure to include the protocol (e.g, http://...)