I have tried to get through this error with no luck. In a form I have a
user enter a title and then search the database for any similar titles.
I need help with this. It’s like the information isn’t being passed
through. Here is my information. I am using Rails 2.1 with
nifty_scaffold. First I changed my route
routes.rb
map.resources :titles, :collection => { :find_similar => :get }
new.html.erb
<% title “New Title” %>
<% form_for @title, :url => { :action => :find_similar } do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.submit "Search" %>
<% end %>
<%= link_to "Back to List", titles_path %>
titles_controller.rb
def find_similar
@searchkey = params[:name]
@title = Title.find(:all,
:conditions => [“name like ?”,
‘%’+@searchkey+’%’])
end
Parameters:
{“commit”=>“Search”,
“title”=>{“name”=>“one day”},
“authenticity_token”=>“ec64448f9d39d29dd3dbafd1a1a31415ea165bdb”}
TypeError in TitlesController#find_similar
can’t convert nil into String
app/controllers/titles_controller.rb:48:in +' app/controllers/titles_controller.rb:48:in
find_similar’
Hi Quincy,
have you tried the following?
def find_similar
@searchkey = params[:title][:name]
…
end
On Jul 12, 8:24 pm, Quincy Central_tex <rails-mailing-l…@andreas-
gsterndale wrote:
Hi Quincy,
have you tried the following?
def find_similar
@searchkey = params[:title][:name]
…
end
On Jul 12, 8:24 pm, Quincy Central_tex <rails-mailing-l…@andreas-
That did it! Thanks gsterndale! Please explain whats happening here. I
haven’t seen this in any documentation.
If you look at the HTML generated by your new.html.erb template you’ll
see how form_for names your input fields. For example:
<% form_for @title, :url => { :action => :find_similar } do |f| %>
<%= f.text_field :name %>
<% end %>
would generate something like:
Note the brackets [] around “name”. As you know, Rails puts the
submitted form field names and values into a Hash called “params”.
When a form field with brackets is submitted, Rails interprets the
value of the parameter “title” as a Hash with a key called “name”. The
value of that field is assigned to that key. The result is a nested
Hash, exactly what you pasted in your first post.
{
“commit”=>“Search”,
“title”=>{
“name”=>“one day”
},
“authenticity_token”=>“ec64448f9d39d29dd3dbafd1a1a31415ea165bdb”
}
On Jul 12, 11:51 pm, Quincy C. [email protected]