Akamaru Akamaru - 4 months ago 15
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 -->
<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 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 %>
</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>


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


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

Answer

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://...)

Comments