1

試合に賭けるユーザーがいます。単一の賭けは「Tipp」と呼ばれ、ユーザーは「tipp.tipp1」と「tipp.tipp2」で一致スコアを予測します

ユーザーの「ヒント」を保存することになっているフォームに問題があります。

以下のコードでは、「accepts_nested_attributes_for:tipps」と「attr_accessible:tipps_attributes」を設定していますが、「保護された属性を一括割り当てできません:tipp」が表示されます。

必要なコードをすべて提供できたと思います。よろしくお願いします!

出力されるパラメーターは次のとおりです。

パラメーター:

{
 "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"
}

短縮されたコード:

コントローラー:

1)ユーザー

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

モデル:

1)ユーザー

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

has_many :tipps
accepts_nested_attributes_for :tipps
end

2)ヒント

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

belongs_to :user
end

私のフォーム:

<%= 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 %>
4

1 に答える 1

2

あなたがしたことをする代わりに、あなたは次のいずれかを試すことができます:

1.代わりに:

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

私はこれを行います:

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

または:2。

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

幸運を

于 2012-09-17T08:53:23.397 に答える