A user has many user_levels and a user_level belongs to a user. We are
having difficulty updating the user_level. Here is the code for
user_level update:

<%= simple_form_for @user do |f| %>
<% @user.user_levels.each do |level| %>
<%= f.fields_for :user_levels, level, :index => level do |
builder| %>
<p><%= render :partial => 'user_levels', :locals => {:f =>
builder, :i_id => level.position} %></p>
<% end %>
<% end %>
<% end %>

The code above will display the current user_level with selected set
to the current position. But the update was not saved for user_level.

Here is the user_levels partial

<div class="fields">
<%= f.input :position, :collection => return_position, :prompt
=> "Choose position",
:label => false, :include_blank =>
true, :selected => i_id %>
<%= link_to_remove_fields "remove", f %>
</div>

Here is the string posted to the server for params[:user]. There are
currently two positions for the user and one gets deleted as update:

{"name"=>"test eng", "login"=>"tester12", "password"=>"password",
"password_confirmation"=>"password", "user_type"=>"employee",
"user_levels_attributes"=>{"0"=>{"id"=>"5"},
"1"=>{"position"=>"elec_eng", "_destroy"=>"false", "id"=>"6"}}}

The app has no problem creating a new user with multiple user_levels.

Any solution for child update? Thanks so much

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Search Discussions

  • Soldier.coder at Feb 17, 2012 at 5:55 am
    I recommend you visit Ryan Bates site RailsCasts.com and watch
    episodes 196 and 197.
    You should go ahead and subscribe, so he will keep doing stuff like
    this.

    Cheers!
    SC
    On Feb 16, 11:37 am, emc_lab wrote:
    A user has many user_levels and a user_level belongs to a user. We are
    having difficulty updating the user_level. Here is the code for
    user_level  update:

    <%= simple_form_for @user do |f| %>
    <% @user.user_levels.each do |level| %>
    <%= f.fields_for :user_levels, level, :index => level do |
    builder| %>
    <p><%= render :partial => 'user_levels', :locals => {:f =>
    builder, :i_id => level.position} %></p>
    <% end %>
    <% end %>
    <% end %>

    The code above will display the current user_level with selected set
    to the current position. But the update was not saved for user_level.

    Here is the user_levels partial

    <div class="fields">
    <%= f.input :position, :collection => return_position, :prompt
    => "Choose position",
    :label => false, :include_blank =>
    true, :selected => i_id %>
    <%= link_to_remove_fields "remove", f %>
    </div>

    Here is the string posted to the server for params[:user]. There are
    currently two positions for the user and one gets deleted as update:

    {"name"=>"test eng", "login"=>"tester12", "password"=>"password",
    "password_confirmation"=>"password", "user_type"=>"employee",
    "user_levels_attributes"=>{"0"=>{"id"=>"5"},
    "1"=>{"position"=>"elec_eng", "_destroy"=>"false", "id"=>"6"}}}

    The app has no problem creating a new user with multiple user_levels.

    Any solution for child update? Thanks so much
    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Peter Vandenabeele at Feb 17, 2012 at 10:27 am

    On Thu, Feb 16, 2012 at 5:37 PM, emc_lab wrote:

    A user has many user_levels and a user_level belongs to a user. We are
    having difficulty updating the user_level. Here is the code for
    user_level update:

    <%= simple_form_for @user do |f| %>
    <% @user.user_levels.each do |level| %>
    <%= f.fields_for :user_levels, level, :index => level do |
    builder| %>
    <p><%= render :partial => 'user_levels', :locals => {:f =>
    builder, :i_id => level.position} %></p>
    <% end %>
    <% end %>
    <% end %>

    The code above will display the current user_level with selected set
    to the current position. But the update was not saved for user_level.

    Here is the user_levels partial

    <div class="fields">
    <%= f.input :position, :collection => return_position, :prompt
    => "Choose position",
    :label => false, :include_blank =>
    true, :selected => i_id %>
    <%= link_to_remove_fields "remove", f %>
    </div>

    Here is the string posted to the server for params[:user]. There are
    currently two positions for the user and one gets deleted as update:

    {"name"=>"test eng", "login"=>"tester12", "password"=>"password",
    "password_confirmation"=>"password", "user_type"=>"employee",
    "user_levels_attributes"=>{"0"=>{"id"=>"5"},
    "1"=>{"position"=>"elec_eng", "_destroy"=>"false", "id"=>"6"}}}

    The app has no problem creating a new user with multiple user_levels.

    Any solution for child update? Thanks so much
    Strange. The params hash seems correct (I did some similar tests in my
    app). If I understand correctly, this should do:

    * leave the user_level with id 5 untouched
    * update the position of user_level with id 6 (if it was changed)

    The "_destroy"=>"false" should not matter

    Which one of the two user_levels gets deleted (with id 5 or id 6).

    If you puts the user.user_levels (in the controller) just before the
    update_attributes line, like below, you can make sure the user_levels
    are still there just before.

    def update

    # add this to debug the status just before the update_attributes
    puts user.user_levels.inspect

    user.update_attributes(params[:user])
    ...
    end

    Also, you could try the update_attributes action in the rails console
    to isolate the problem.
    user = User.find ...
    user.user_levels
    h = {"name"=>"test eng", "login"=>"tester12", "password"=>"password",
    "password_confirmation"=>"password", "user_type"=>"employee",
    "user_levels_attributes"=>{"0"=>{"id"=>"5"},
    "1"=>{"position"=>"elec_eng", "_destroy"=>"false", "id"=>"6"}}}
    user.update_attributes(h)
    user.user_levels
    That is how tested the expected behavior (of course with different model
    names for my app).

    HTH,

    Peter


    --
    *** Available for a new project ***

    Peter Vandenabeele
    http://twitter.com/peter_v
    http://rails.vandenabeele.com
    http://coderwall.com/peter_v

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedFeb 16, '12 at 4:38p
activeFeb 17, '12 at 10:27a
posts3
users3
websiterubyonrails.org
irc#RubyOnRails

People

Translate

site design / logo © 2021 Grokbase