-1

I have a nested form that does not update the database.

Models:

User:

has_many :designs, :dependent => :restrict

accepts_nested_attributes_for :designs

attr_accessible :designs_attributes

Design:

belongs_to :user

Designs Controller:

def update
    @design = Design.find(params[:id])
    @user = User.find_by_id(@design.user_id)
    @points = (@user.points + 5000)
    if @design.update_attributes(params[:design])
        flash[:success] = "Design aproved"
        redirect_to designindex_path
    else
        @title = @design.name + %(, created by ) + @design.user.username
        render 'edit'
    end
end

And here is the form:

<%= form_for @user, :html => { :multipart => true } do |f| %>
    <%= f.hidden_field(:points, {:value => @points}) %>
    <%= f.fields_for :designs, @design do |d| %>
        <div class="field">
            <%= d.label :total_price, %(New price) %><br />
            <%= d.text_field :total_price %>
        </div>
        <%= d.hidden_field(:aproved, {:value => 1}) %>
    <% end %>
    <div class="actions">
        <%= f.submit "Submit" %>
    </div>
<% end %>

Also, when I push Submit button, it redirects me to root_path.

Looking in the console output I found this:

Started POST "/users/101" for 127.0.0.1 at 2011-09-29 15:31:16 +0200
Processing by UsersController#update as HTML
Parameters: {"utf8"=>"✓",
"authenticity_token"=>"N16nb5i/PUPftGHs3uvnnLwrp8e/PQB88w0OU6DLGko=",
"user"=>{"points"=>"7500", "designs_attributes"=>{"0"=>{"total_price"=>"20",
"aproved"=>"1", "id"=>"66"}}}, "commit"=>"Submit", "id"=>"101"}

The problem here seems to be the following:

An user has_many designs, but when doing the POST request the controller does not specify which one is being updated. Anyway, the users.points attribute is neither updated.

Update

Ok, I got the problem, I have this in users_controller.rb:

before_filter :authenticate, :only => [:index, :edit, :update, :destroy]
before_filter :correct_user, :only => [:edit, :update]

And I'm trying to update another user's information from an admin account. Can I do this in any way?

Here is the code for those:

def correct_user
    @user = User.find(params[:id])
    redirect_to(root_path) unless current_user?(@user)
end

def current_user?(user)
    user == current_user
end

def authenticate
    deny_access unless signed_in?
end

def signed_in?
    !current_user.nil?
end
1
  • You'll need to post the contents of correct_user/authenticate to tell. but it definitely seems to be your before_filter if you're being redirected. Sep 29, 2011 at 15:45

2 Answers 2

0

You'll want to do:

<%= f.fields_for :designs, @design do |d| %>
  ...
<% end %>

This will make sure it uses the designs_attributes attribute (and will default to 0 index I believe). Can't remember if you'll need to wrap the @design in an array ([@design]), don't believe you do.

Your params should look like {:user => {:designs_attributes => { '0' => {:id => 1, ... } } } }

2
  • Tried that (both @design and [@design]) and is not working either... I'll update the cuestion now with the new code and params.
    – mornaner
    Sep 29, 2011 at 13:33
  • You'll need to add :points to attr_accessible as well to make it setable. Can you add attr_accessible to all the fields you need on Design as well. Your params are looking good, it is specifying the design you want to update based on the id being passed in Sep 29, 2011 at 14:24
0

Ok, got it working, just modified correct_user function as follows

def correct_user
    @user = User.find(params[:id])
    redirect_to(root_path) unless current_user?(@user) || current_user.admin
end

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.