Iterate over a collection/save associated model

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 :state

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 %>

State Table
id
name

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.

belongs_to :states should be :state singular.

On Tue, Apr 12, 2016 at 10:47 PM, David W. [email protected]

shahroon ali wrote in post #1182801:

belongs_to :states should be :state singular.

On Tue, Apr 12, 2016 at 10:47 PM, David W. [email protected]

I fixed the typo, but it’s still not working.

undefined method `state_id’ for #Profile:0xd1b0b00

Colin L. wrote in post #1182803:

On 12 Apr 2016 6:58 p.m., “David W.” [email protected] wrote:

shahroon ali wrote in post #1182801:

belongs_to :states should be :state singular.

On Tue, Apr 12, 2016 at 10:47 PM, David W. [email protected]

I fixed the typo, but it’s still not working.

undefined method `state_id’ for #Profile:0xd1b0b00

Is there a column of that name in the profiles table?

Colin


Posted via http://www.ruby-forum.com/.

I thought the table automatically creates the _id extension by default.

My categories table is nearly identical to the state table.

categories
id
name
created_at
updated_at
description
slug

On 12 Apr 2016 6:58 p.m., “David W.” [email protected] wrote:

shahroon ali wrote in post #1182801:

belongs_to :states should be :state singular.

On Tue, Apr 12, 2016 at 10:47 PM, David W. [email protected]

I fixed the typo, but it’s still not working.

undefined method `state_id’ for #Profile:0xd1b0b00

Is there a column of that name in the profiles table?

Colin


Posted via http://www.ruby-forum.com/.


You received this message because you are subscribed to the Google G.
“Ruby on Rails: Talk” group.
To unsubscribe from this group and stop receiving emails from it, send an
email to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/rubyonrails-talk/9bf1492db35264d858ecc0debcfb1de9%40ruby-forum.com
.

On 12 April 2016 at 19:10, David W. [email protected] wrote:

undefined method `state_id’ for #Profile:0xd1b0b00

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.

Colin

I fixed it, thanks guys. The state_id was missing on the profile table.

Colin L. wrote in post #1182808:

On 12 April 2016 at 19:10, David W. [email protected] wrote:

undefined method `state_id’ for #Profile:0xd1b0b00

What has the categories table got to do with it? I thought this
problem is about profiles and states.

Colin

I made a small mistake, I’ve been working on a lot lately. I forgot to
add it to the table.