melomane
2009-Jul-16 18:23 UTC
Merge error in collection_select when trying to access another model
Hi I am a newbie in rails. My question may seems too simple to be discussed here, but the only place to get my answer from experts is here. I searched in this group, but didn''t find something like my problem, although there were some problems about merge error. I have two models here, links, and subjects. every link must have a subject. So a subject has_many links and links belongs_to a subject. This is my new.html.erb of links: <h1>New link</h1> <% form_for(@link) do |f| %> <%= f.error_messages %> <p> <%= f.label :url %><br /> <%= f.text_field :url %> </p> <%= f.collection_select(:subject, :id, @subjects, :id, :title , {:prompt => "Select a subject"}) %> <p> <%= f.submit ''Create'' %> </p> <% end %> (in links controller, I added the second line to the scaffold- generated controller to access the subject: def create 1 @link = Link.new(params[:link]) 2 @link.subject_id = params[:subject][:id] 3 respond_to do |format| 4 if @link.save ... ) in collection_select, user must choose subject of the link. It only works when I omit "f." in f.collection_select although it belongs to this form, f. Otherwise I get this error message: undefined method `merge'' for :title:Symbol I don''t know the reason. I removed the f. to make the application work. I am not sure if this other problem is related to the previous one or not. Now, when I add validates_presence_of :url in my link model, in order to prevent the user to leave this field empty, instead of showing a flash when it is empty, I get this error messege: You have a nil object when you didn''t expect it! You might have expected an instance of Array. The error occurred while evaluating nil.map And it points to the line which I wrote collection_select. I can''t realise the relation of url field to collection_select! I appreciate everyone who helps me :)