I have a profile model that has a user state model (emotional state) -
What I would like to do is give the user object the ability to change
their state through the profile model. The code that I've written is
below. I'm receiving "NoMethodError - undefined method 'state_id' for
#<Profile:Obj %>" even though, they're associated with each other.
Profile.rb
belongs_to :states
State.rb
has_many :profiles
_state_form.html.erb
<%= simple_form_for @user, url: current_user, html: {multipart: true},
layout: :horizontal do |f| %>
<%= f.simple_fields_for :profile do |n| %>
<%= n.label :state_id, 'Select a state' %>
<%= n.collection_select :state_id, State.all, :id, :name %>
<%= f.submit 'Change State', class: 'btn btn-info-outline' %>
<% end %>
<% end %>
I seeded the names of the states into the db table. Because, I don't
want anyone altering the records to add their own.
<%= render 'users/shared/state_form' %>
Maybe, I'm missing something, but this approach seems to work without
issue for categories.
I thought the table automatically creates the _id extension by default.
How can a table automatically create a column? If you have profile
belongs_to state then you have to create a migration to put the
state_id column in the database. I think you need to follow the
advice that has now nearly worn out my keyboard, so many times have I
typed it: You should work right through a good tutorial such as
railstutorial.org (which is free to use online). That will show you
the basics of Rails.
My categories table is nearly identical to the state table.
What has the categories table got to do with it? I thought this
problem is about profiles and states.