1

I have Users who bet on matches. A single bet is called "Tipp" and the users predict the match score in "tipp.tipp1" and "tipp.tipp2"

I have problems with my form which is supposed to save "tipps" of users.

With the code below I get "Can't mass-assign protected attributes: tipp" although i have set "accepts_nested_attributes_for :tipps" and "attr_accessible :tipps_attributes".

I hope I have provided all the necessary code. Thanks in advance for your help!

Here is the parameters output:

Parameters:

{
 "utf8"=>"✓",
 "_method"=>"put",
 "authenticity_token"=>"mPPpCHjA3f/M2l1Bd3ffO1QUr+kdETGkNE/0CNhbJXE=",
 "user" =>{
          "tipp"=>{
                    "6"=>{"tipp1"=>"4","tipp2"=>"6"},
                    "7"=>{"tipp1"=>"-1","tipp2"=>"-1"},
                    "8"=>{"tipp1"=>"-1","tipp2"=>"-1"}
                  }
            },
 "commit"=>"Update User",
 "user_id"=>"1"
}

Shortened Code:

Controllers:

1) Users

class UsersController < ApplicationController

def edit_tipps
    @user = current_user
end

def update_tipps
    @user = current_user
    if @user.update_attributes(params[:user])
        flash[:notice] = "success (maybe)"
        redirect_to user_edit_tipps_path(@user)
    else
        flash[:error] = "errors"
        redirect_to user_edit_tipps_path(@user)
    end
end

Models:

1) Users

class User < ActiveRecord::Base 
attr_accessible :email, :password, :password_confirmation, :tipps_attributes

has_many :tipps
accepts_nested_attributes_for :tipps
end

2) Tipps

class Tipp < ActiveRecord::Base
attr_accessible :match_id, :points, :round_id, :tipp1, :tipp2, :user_id

belongs_to :user
end

My Form:

<%= form_for @user, :url => { :action => "update_tipps" }  do |user_form| %>
    <% @user.tipps.each do |tipp| %>
    <%= user_form.fields_for tipp, :index => tipp.id do |tipp_form|%>
        <%= tipp_form.text_field :tipp1 %><br/>
        <%= tipp_form.text_field :tipp2 %><br/>
    <% end %>
    <% end %>
    <%= submit_or_cancel(user_form) %> 
<% end %>

1 Answer 1

2

Instead of doing what you did, you could try either:

1. Instead of:

  <% @user.tipps.each do |tipp| %>
  <%= user_form.fields_for tipp, :index => tipp.id do |tipp_form|%>

I would do this:

  <%= user_form.fields_for :tipps do |tipp_form| %>

Or: 2.

class User < ActiveRecord::Base 
attr_accessible :email, :password, :password_confirmation, :tipps_attributes, :tipps

Goodluck

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.