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!