Note that there are some explanatory texts on larger screens.

plurals
  1. PONested form not updating the database
    primarykey
    data
    text
    <p>I have a nested form that does not update the database.</p> <p><strong>Models:</strong></p> <p>User:</p> <pre><code>has_many :designs, :dependent =&gt; :restrict accepts_nested_attributes_for :designs attr_accessible :designs_attributes </code></pre> <p>Design: </p> <pre><code>belongs_to :user </code></pre> <p><strong>Designs Controller:</strong></p> <pre><code>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 </code></pre> <p>And here is the form:</p> <pre><code>&lt;%= form_for @user, :html =&gt; { :multipart =&gt; true } do |f| %&gt; &lt;%= f.hidden_field(:points, {:value =&gt; @points}) %&gt; &lt;%= f.fields_for :designs, @design do |d| %&gt; &lt;div class="field"&gt; &lt;%= d.label :total_price, %(New price) %&gt;&lt;br /&gt; &lt;%= d.text_field :total_price %&gt; &lt;/div&gt; &lt;%= d.hidden_field(:aproved, {:value =&gt; 1}) %&gt; &lt;% end %&gt; &lt;div class="actions"&gt; &lt;%= f.submit "Submit" %&gt; &lt;/div&gt; &lt;% end %&gt; </code></pre> <p>Also, when I push Submit button, it redirects me to root_path.</p> <p>Looking in the console output I found this:</p> <pre><code>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"=&gt;"✓", "authenticity_token"=&gt;"N16nb5i/PUPftGHs3uvnnLwrp8e/PQB88w0OU6DLGko=", "user"=&gt;{"points"=&gt;"7500", "designs_attributes"=&gt;{"0"=&gt;{"total_price"=&gt;"20", "aproved"=&gt;"1", "id"=&gt;"66"}}}, "commit"=&gt;"Submit", "id"=&gt;"101"} </code></pre> <p>The problem here seems to be the following:</p> <p>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.</p> <p><strong>Update</strong></p> <p>Ok, I got the problem, I have this in users_controller.rb:</p> <pre><code>before_filter :authenticate, :only =&gt; [:index, :edit, :update, :destroy] before_filter :correct_user, :only =&gt; [:edit, :update] </code></pre> <p>And I'm trying to update another user's information from an admin account. Can I do this in any way?</p> <p>Here is the code for those:</p> <pre><code>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 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload