Rails: Display database query on HTML page, ActiveRecord -
i trying create search form rails app, user enters query , based off query, want them see list of links associated query (these links come 'links' table). created html form , has user enter query. use query
<%= form_tag links_path, :method => 'get' %> <p> <%= text_field_tag :search, params[:search] %> <%= submit_tag "search", :name => nil %> </p> <% end %>
in controller links table, have if
statement checks user has entered , assigns link.where('title ?', '%{#params[:search]}%')
@links
. , converts array (.to_a
)
here statement in index action:
def index @links = link.all if params[:search] #@links = link.find(:all, :conditions => ['title ?', '%{#params[:search]}%']) @links = link.where('title ?', '%{#params[:search]}%') @links.to_a end
end
in index.html.erb
display result. used <%= @links %>
however, displays activerecord: #<link::activerecord_relation:0x0000000d5c69f0>
how convert query result activerecord array, able index it? thanks.
you need assigns @links this:
@links = @links.to_a
by way, if want render link one-by-one using @links.each, not need convert @links array.
Comments
Post a Comment