Rails Form with belongs_to Association
Asked Answered
F

2

5

I'm very new to Rails so this may be an obvious problem, and if so I apologize.

I am trying to create a form for creating a User record, which has a belongs_to association with a Team model. What I've done so far is the following...

<% form_for @user, url: {action: "create"} do |f| %>
  <%= f.text_field :name %>
  <%= f.text_field :email %>

  <% f.fields_for :team do |team| %>
    <%= team.collection_select(:team_id, Team.all, :id, :name) %>
  <% end %>
  <%= f.submit %>
<% end %>

This seems to work well enough, but when creating the User record I'm running into trouble.

def create
  @team = Team.find(params[:user][:team][:team_id])
  @team.users.create(user_params)
  # Ignoring error checking for brevity
end

def user_params
    params.require(:user).permit(:name, :email)
end

The params now contains a field for team_id which is not an attribute of the User model, and thus the creation fails. I'm not sure how to go about addressing that, let alone whether or not this is the appropriate way to approach this. Any advice would be greatly appreciated!

Facsimile answered 29/8, 2015 at 23:58 Comment(2)
If a user belongs_to a team it should have a team_id since belongs_to is in the side with the foreign key? Also you don't need the fields_for unless you where doing something with nested attributes.Arapaho
I'm sorry, I wasn't entirely aware of how the belongs_to association actually works, you're right. Thanks!Facsimile
B
13

welcome to Rails :)

There is no problem with your logic of going about the association this way if the goal is to make sure that each user can be part of a team.

So first you'll need to make sure that team_id exists on the user model. And seoncdly, as Doon suggested, you don't need fields_for unless you want to interact with the team model and make changes from within that same form.

So first create a migration rails g migration add_team_to_user team:belongs_to

using belongs_to in your migration will add a reference, which you can learn about here: http://edgeguides.rubyonrails.org/active_record_migrations.html

Then migrate your database bundle exec rake db:migrate

and restart your server. Then change your form like so:

<% form_for @user, url: {action: "create"} do |f| %>
  <%= f.text_field :name %>
  <%= f.text_field :email %>
  <%= f.collection_select(:team_id, Team.all, :id, :name) %>

  <%= f.submit %>
<% end %>
Bandurria answered 30/8, 2015 at 0:12 Comment(0)
K
2

easy to do this use the gem https://github.com/plataformatec/simple_form

Associations

To deal with associations, Simple Form can generate select inputs, a series of radios buttons or checkboxes. Lets see how it works: imagine you have a user model that belongs to a company and has_and_belongs_to_many roles. The structure would be something like:

class User < ActiveRecord::Base
  belongs_to :company
  has_and_belongs_to_many :roles
end

class Company < ActiveRecord::Base
  has_many :users
end

class Role < ActiveRecord::Base
  has_and_belongs_to_many :users
end

Now we have the user form:

<%= simple_form_for @user do |f| %>
  <%= f.input :name %>
  <%= f.association :company %>
  <%= f.association :roles %>
  <%= f.button :submit %>
<% end %>
Kindhearted answered 31/8, 2015 at 1:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.