0

I'm stuck with this issue. The RoR doc about this topic is really awful. I don't understand how collection_select works.

I have two models : Skills and Projects. A Project have many skills and a skill have many projects.

So here are my schemas :

create_table "projects", force: true do |t|
    t.string   "name"
    t.datetime "created_at"
    t.datetime "updated_at"
end

create_table "projects_skills", id: false, force: true do |t|
    t.integer "project_id"
    t.integer "skill_id"
end

create_table "skills", force: true do |t|
    t.string   "name"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.integer  "mastering"
end

The Project Model

class Project < ActiveRecord::Base

    has_and_belongs_to_many :skills

end

The Skill Model

class Skill < ActiveRecord::Base

    has_and_belongs_to_many :projects

end

And the collection_select

= form_for(instance_variable_get('@' + controller.controller_name.singularize)) do |f|

    .field

        = f.label :skills
        = collection_select(:skills, :id, Skill.all, :id, :name, {:multiple=>true})

    .actions

        = f.submit

The select appears well populated, but nothing is persisted in my database.

Maybe someone can see a mistake in my code ?

Thanks

  • Your code is fine as far as I can tell. You're saying the `select` box appears properly populated, but can you explain what you mean by *nothing is persisted in my database*? Do you mean when you submit the form, no skills are saved for that project? – Paul Richter Jul 01 '14 at 16:15
  • 1
    I just realized, you forgot to put `f.` in front of the `collection_select`. Is that a typo in the question or how it appears in the form? If the latter, that could be your problem. – Paul Richter Jul 01 '14 at 16:15
  • 1
    Yep Paul that's exactly my issue. It seems that both f.collection_select and collection_select are acceptable. And frankly, I don't know the difference... –  Jul 01 '14 at 16:29
  • As I said, the form is here, the select is well populated but when I submit, the skills selected are not saved for this project. I tried f.collection_select too. –  Jul 01 '14 at 16:32
  • According to the documentation, the first argument should be an object, not a collection. So try :skill instead of :skills. That will render – Tiago Farias Jul 01 '14 at 16:38
  • In which field are you expecting the values to be saved? – Pavan Jul 01 '14 at 16:48
  • 1
    Can you post the `create` action controller code for this resource (I'm assuming its `Project`). You're right that both are acceptable, but the difference is in how the parameter map is built. If you do it the way I indicated, you will end up with the `skills` parameter inside of the overall `project` parameter list, which is usually desirable. That's why I'd like to see the `create` action because the problem could be in how you're constructing the object. – Paul Richter Jul 01 '14 at 16:48
  • If you are expecting the values to be saved in `skill_id`,then you should give it like this `=f.collection_select(:skill_id, Skill.all, :id, :name, {:multiple=>true})` – Pavan Jul 01 '14 at 16:56
  • Pavan, I get an undefined method `skill_id' when I do this. Paul, there is nothing special on the create method of Project. I thought the relations saving was automatic, this is not the case ? –  Jul 01 '14 at 17:09
  • Are you using it in `form_for`? Please post the full code. – Pavan Jul 01 '14 at 17:10
  • Yep I'm using form_for, I edited to post it. –  Jul 01 '14 at 17:17
  • You will also need to use `accepts_nested_attributes_for` in order to populate the join table. This post should have everything you need to get it working: http://stackoverflow.com/questions/1847641/trying-to-use-accepts-nested-attributes-for-and-has-and-belongs-to-many-but-the – infused Jul 01 '14 at 17:20
  • 1
    `= form_for(instance_variable_get('@' + controller.controller_name.singularize)) do |f|`? a bit complicated.I expected something like this `= form_for(@skill) do |f|`. – Pavan Jul 01 '14 at 17:20
  • It's the same, just a generic way to do it. I only have one form for all my models, and my fields are partials. –  Jul 01 '14 at 17:23

0 Answers0