사용자와 프로필 사이에 has_one 연관이 있고 accepts_nested_attributes가 있습니다. 모든 매개 변수가 로그에 따라 전달되지만 unpermitted parameter profile
으로 계속 실행됩니다.has_one 관련 중첩 리소스를 저장하는 방법은 무엇입니까? 허용되지 않는 매개 변수 오류
사용자 모델
class User < ActiveRecord::Base
searchkick
validates_presence_of :first_name, :last_name, :phone_number
VALID_EMAIL_REGEX = /\A[\w+\-.][email protected][a-z\d\-]+(?:\.[a-z\d\-]+)*\.[a-z]+\z/i
validates :email, presence: true,
format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
has_one :profile, dependent: :destroy
accepts_nested_attributes_for :profile
validates_associated :profile
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
def name
[first_name, last_name].compact.join(' ')
end
end
프로필 모델
class Profile < ActiveRecord::Base
belongs_to :user
mount_uploader :identification_recto, FileUploader
mount_uploader :identification_verso, FileUploader
mount_uploader :proof_of_income_1, FileUploader
mount_uploader :proof_of_income_2, FileUploader
mount_uploader :proof_of_income_3, FileUploader
validates_presence_of :user_id
end
사용자 컨트롤러
class Teller::UsersController < TellerController
def new
@user = User.new
@user.build_profile
end
def create
@user = User.new create_params
@user.enroller_id = current_user.id
# @profile.user_id = @user.id
save_user or render 'new'
end
private
def load_user
@user ||= user_scope.find(params[:id])
end
def user_scope
User.where(nil)
end
def save_user
if @user.save
flash[:success] = "C'est fait!"
redirect_to teller_root_path
end
end
def create_params
params.require(:user).permit(:first_name, :last_name,
:email, :password, :phone_number, :succ, :teller,
profile_attributes: [:identification_recto,
:identification_verso,
:proof_of_income_1,
:proof_of_income_2,
:proof_of_income_3])
end
end
형태
<%= render 'shared/error_messages', object: f.object %>
<%= f.text_field :first_name, placeholder: "Prénoms", class: "form-control" %> <br />
<%= f.text_field :last_name, placeholder: "Nom", class: "form-control" %> <br />
<%= f.text_field :email, placeholder: "email", class: "form-control" %> <br />
<%= f.password_field :password, placeholder: "Mot de passe - 8 characteres min", class: "form-control" %> <br />
<%= f.text_field :phone_number, placeholder: "Numero de téléphone", class: "form-control" %> <br />
<span class="form-control">
<%= f.check_box :succ %> Succursale
</span> <br />
<span class="form-control">
<%= f.check_box :teller %> Agent
</span> <br />
<div class="panel panel-default">
<%= f.fields_for [:profile, @user.build_profile] do |builder| %>
<%= render 'shared/profile_fields', f: builder %>
<% end %>
</div>
로그인
<div class="panel-heading">
<h5 class="text-uppercase">Profile</h5>
</div>
<fieldset class="panel-body">
<h4 class="text-center">
Identification
</h4>
<%= f.file_field :identification_recto, placeholder: "Upload", class: "form-control" %>
<br />
<%= f.file_field :identification_verso, placeholder: "Upload", class: "form-control" %>
<h4 class="text-center">
Sources de revenus
</h4>
<%= f.file_field :proof_of_income_1, class: "form-control" %>
<br />
<%= f.file_field :proof_of_income_2, class: "form-control" %>
<br />
<%= f.file_field :proof_of_income_3, class: "form-control" %>
<br />
</fieldset>
부분
프로필 필드는
Parameters: {"utf8"=>"✓", "authenticity_token"=>"TniskJATOw8LLh9jUSk28sV4RfKnwUH9ngExOE4TPtPSf/u5cYF/7b7WEu3mKfvgMFDD4hk+78u6tDkr/IEEuw==", "user"=>{"first_name"=>"Aziz", "last_name"=>"Test", "email"=>"[email protected]", "password"=>"[FILTERED]", "phone_number"=>"+1 416 388 94 49", "succ"=>"0", "teller"=>"0", "profile"=>{"identification_recto"=>#<ActionDispatch::Http::UploadedFile:0x007f8640a10c98 @tempfile=#<Tempfile:/var/folders/8x/x82yrjys3_n11t6ktj4mx8xr0000gn/T/RackMultipart20160714-57689-iuz82p.png>, @original_filename="headline-lockup.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"user[profile][identification_recto]\"; filename=\"headline-lockup.png\"\r\nContent-Type: image/png\r\n">, "identification_verso"=>#<ActionDispatch::Http::UploadedFile:0x007f8640a10c48 @tempfile=#<Tempfile:/var/folders/8x/x82yrjys3_n11t6ktj4mx8xr0000gn/T/RackMultipart20160714-57689-kzsycl.png>, @original_filename="headline-lockup.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"user[profile][identification_verso]\"; filename=\"headline-lockup.png\"\r\nContent-Type: image/png\r\n">, "proof_of_income_1"=>#<ActionDispatch::Http::UploadedFile:0x007f8640a10bf8 @tempfile=#<Tempfile:/var/folders/8x/x82yrjys3_n11t6ktj4mx8xr0000gn/T/RackMultipart20160714-57689-1navy8v.png>, @original_filename="headline-lockup.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"user[profile][proof_of_income_1]\"; filename=\"headline-lockup.png\"\r\nContent-Type: image/png\r\n">, "proof_of_income_2"=>#<ActionDispatch::Http::UploadedFile:0x007f8640a10ba8 @tempfile=#<Tempfile:/var/folders/8x/x82yrjys3_n11t6ktj4mx8xr0000gn/T/RackMultipart20160714-57689-1cfuegj.png>, @original_filename="headline-lockup.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"user[profile][proof_of_income_2]\"; filename=\"headline-lockup.png\"\r\nContent-Type: image/png\r\n">}}, "commit"=>"Je valide"}
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
Unpermitted parameter: profile
(0.1ms) BEGIN
User Exists (0.5ms) SELECT 1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER('[email protected]') LIMIT 1
User Exists (0.3ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = '[email protected]' LIMIT 1
SQL (0.3ms) INSERT INTO "users" ("first_name", "last_name", "email", "encrypted_password", "phone_number", "enroller_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING "id" [["first_name", "Aziz"], ["last_name", "Test"], ["email", "[email protected]"], ["encrypted_password", "$2a$11$VR7dPZ7cJkZQP1Bxm/2fXO.2BtUHJbLd2Rp.9vjBQQEr5AsZt5fF."], ["phone_number", "+221 76 388 94 49"], ["enroller_id", 1], ["created_at", "2016-07-14 08:53:00.023114"], ["updated_at", "2016-07-14 08:53:00.023114"]]
모든 입력은 환영합니다. 당신의 도움을
주 감사 : 나는 아무 소용이
덕분에이 has_one nested attributes not saving 살펴 있었다!
서버에서 얻는 paremeter 구조는 무엇입니까? –
'<% = f.fields_for [: profile, @ user.build_profile] do | 빌더를 변경해보십시오. %>'~'<% = f.fields_for : profile do | 빌더 | %>' – Pavan
@Pavan : 그게 올바른 방법 인 것 같습니다. 다음 오류가 발생합니다. '프로필 사용자는 비워 둘 수 없습니다'. 그러나 컨트롤러에 설정을 '@ profile.user_id'로 설정하려면 어떻게해야합니까? 설정하지 않고는 아무 것도 언급 할 수 없으므로 – Ghost