0

Hi i have been trying to make a sample inventory system to understand rails more, but the problem is that a lot of video's is a bit messy and i cant wrap my head around how the us and make the associations + the database with the collection_select.

what i generally want to make is a an Item that has a name , description and belongs to a category, then the category has a name,

Item -> name:string description:string category_id:integer Category -> name:string

so that i can learn more about associations i want the item that belongs to a category dynamic , i mean i can add 10 things in the category and then you can choose from the 10 categories that you added into the item.

but i can't properly wrap and make this , can someone help me code this , as i am a bit new on rails so i can't properly do this.

can you pls add how to make this, as it would really be a great learning curve as the other video's that i saw is not working when i try to do it. any help would realy be appreciated

user1868185
  • 899
  • 3
  • 9
  • 24

1 Answers1

0

that is pretty simple to do

in your _form.html.erb you have to do this

<%= f.select(:categories, Category.all.collect {|c| [ c.name, c.id ] }, {}, { multiple: true , class: "form-control" })%>
</div>

to whitelist all the categories params you have to change your controller

def item_params
params.require(:item).permit(:name, :description, :categoriess => [:id ])

end

Lalit Yadav
  • 889
  • 9
  • 16