Ruby on Rails Tuesday, April 12, 2016

belongs_to :states should be :state singular.

On Tue, Apr 12, 2016 at 10:47 PM, David Williams <lists@ruby-forum.com> wrote:
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.

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

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/33864495c9bc99a4e62a62dfd435f649%40ruby-forum.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAGxm8evPCHyChmmkpBQ2f7_vTwqKq3PzjpLQC5SC%3D-xA7CDghQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment