Possible Duplicate:
I want to return a single result using .find() with Ruby on Rails
This is my code
<%= f.select :user_id, User.find(:user_id).collect {|t|
[t.user_name, t.id]} %>
which gives the output
Couldn't find User with id=user_id
It doesn’t seem to pass the variable instead it’s passing ‘user_id’ as a string.
I just want it to return a single result into either a select list or label
I’m fairly new to ROR, I might be going about this the completely wrong way. Any help is much appreciated.
You are passing :user_id to the find method, which is a symbol, not a user id.
You have to pass a number to find, then find will search for the record with that id.
Another problem, why are you calling collect on the result of find ? Find return only one row, not an array.
Here is how you should structure your code :
You should get the user variable into the controller, for instance :
@user = User.find(params[:id])Beware, find throws an exception if the model is not found, as you saw, consider using find_by_id that returns nil if no model is found.
Then, you can use @user in your view.
If you want to display a select tag with only the user in it, do the following :
<%= f.select, :user_id, options_for_select([[@user.user_name, @user.id]]) %>Check http://guides.rubyonrails.org/form_helpers.html for form helpers.