Reputation: 133
I tried to list out the values of a specific table in the database using the following view file
index.html.erb
<%= form_tag univ_path, method: :get do %>
<p>
<%= text_field_tag :query, params[:query] %>
<%= submit_tag "Search", name: nil %>
</p>
<% end %>
<h1>List of Universities based on ranking</h1>
<div id="articles">
<% @univ.each do |univ| %>
<h2>
<%= link_to univ.univ_name, univ %>
</h2>
<div class="info">
<b>Location:</b> <%= univ.location %>, <%= univ.state %> <br/>
</div>
<br /><br />
<div class="content"><p><%= univ.description %><p></div>
<% end %>
</div>
and the controller file is as follows
univ_controller.rb
class UnivController < ApplicationController
def index
if params[:query].present?
@univ= Univ.search(params[:query])
else
@univ=Univ.all
end
end
def show
@univ= Univ.find(params[:id])
end
def register
@univ = Univ.new(params[:univ])
if(request.post? and @univ.save)
flash[:notice]= "Account Created Successfully"
redirect_to :controller => 'univ', :action => 'register'
end
end
end
I was able to list out the contents of the table from db. However, when I include the form_tag for implementing search it results an error
No route matches {:action=>"show", :controller=>"univ"} missing required keys: [:id]
my routes.rb has
resources :univ get 'search' => "univ#index", :as => "search"
Please suggest necessary changes
Upvotes: 1
Views: 1150
Reputation: 3
I just had this problem with a project I'm working on, and it turned out to be a Nil field in my database. Are you saving data to ruby sqlite? Check for a record that has a nil [:id] and delete that record using Model.destroy(id) and you should be able to render the page again
Upvotes: 0
Reputation: 53048
Change the form_for
in index.html.erb
as below:
<%= form_tag search_path, method: :get do %>
I suppose you intended to submit the form on index action using the search
route defined as:
get 'search' => "univ#index", :as => "search"
for that you would have to submit the form on search_path
.
You are getting the error as No route matches {:action=>"show", :controller=>"univ"} missing required keys: [:id]
because currently you are submitting the form on univ_path
which would route to show
action and therefore require an id
.
Upvotes: 1