연결된 새 기본 설정 has_one 연결을 저장하지 못했습니다.

ARTLoe

has_one 기본 설정에 대한 중첩 모델 양식을 만들려고합니다. (저는 Rails 4를 사용하고 있습니다)

하지만 해결 방법을 이해하지 못하는 아래 오류가 발생합니다. 이것은 사용자가 기본 설정 양식을 작성하지 않은 경우에 발생합니다 (기본 설정 속성에 대한 유효성 검사가있는 경우).여기에 이미지 설명 입력

오류의 터미널 표시

ActiveRecord::RecordNotSaved - Failed to save the new associated preference.:
  activerecord (4.2.0) lib/active_record/associations/has_one_association.rb:43:in `block in replace'
  activerecord (4.2.0) lib/active_record/associations/has_one_association.rb:98:in `block in transaction_if'
  activerecord (4.2.0) lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `block in transaction'
  activerecord (4.2.0) lib/active_record/connection_adapters/abstract/transaction.rb:188:in `within_new_transaction'
  activerecord (4.2.0) lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `transaction'
  activerecord (4.2.0) lib/active_record/transactions.rb:220:in `transaction'
  activerecord (4.2.0) lib/active_record/associations/has_one_association.rb:98:in `transaction_if'
  activerecord (4.2.0) lib/active_record/associations/has_one_association.rb:33:in `replace'
  activerecord (4.2.0) lib/active_record/associations/singular_association.rb:17:in `writer'
  activerecord (4.2.0) lib/active_record/associations/builder/association.rb:123:in `preference='
  app/controllers/preferences_controller.rb:55:in `create'
  actionpack (4.2.0) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
  actionpack (4.2.0) lib/abstract_controller/base.rb:198:in `process_action'
  actionpack (4.2.0) lib/action_controller/metal/rendering.rb:10:in `process_action'
  actionpack (4.2.0) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
  activesupport (4.2.0) lib/active_support/callbacks.rb:117:in `call'
  activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `block in halting'
  activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `block in halting'
  activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `block in halting'
  activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `block in halting'
  activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `block in halting'
  activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `block in halting'
  activesupport (4.2.0) lib/active_support/callbacks.rb:151:in `block in halting_and_conditional'
  activesupport (4.2.0) lib/active_support/callbacks.rb:151:in `block in halting_and_conditional'
  activesupport (4.2.0) lib/active_support/callbacks.rb:151:in `block in halting_and_conditional'
  activesupport (4.2.0) lib/active_support/callbacks.rb:234:in `block in halting'
  activesupport (4.2.0) lib/active_support/callbacks.rb:308:in `block (2 levels) in halting'
  public_activity (1.5.0) lib/public_activity/utility/store_controller.rb:25:in `store_controller_for_public_activity'
  activesupport (4.2.0) lib/active_support/callbacks.rb:427:in `block in make_lambda'
  activesupport (4.2.0) lib/active_support/callbacks.rb:307:in `block in halting'
  activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `block in halting'
  activesupport (4.2.0) lib/active_support/callbacks.rb:234:in `block in halting'
  activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `block in halting'
  activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `block in halting'
  activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `block in halting'
  activesupport (4.2.0) lib/active_support/callbacks.rb:92:in `_run_callbacks'
  activesupport (4.2.0) lib/active_support/callbacks.rb:734:in `_run_process_action_callbacks'
  activesupport (4.2.0) lib/active_support/callbacks.rb:81:in `run_callbacks'
  actionpack (4.2.0) lib/abstract_controller/callbacks.rb:19:in `process_action'
  actionpack (4.2.0) lib/action_controller/metal/rescue.rb:29:in `process_action'
  actionpack (4.2.0) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
  activesupport (4.2.0) lib/active_support/notifications.rb:164:in `block in instrument'
  activesupport (4.2.0) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
  activesupport (4.2.0) lib/active_support/notifications.rb:164:in `instrument'
  actionpack (4.2.0) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
  actionpack (4.2.0) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
  activerecord (4.2.0) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
  actionpack (4.2.0) lib/abstract_controller/base.rb:137:in `process'
  actionview (4.2.0) lib/action_view/rendering.rb:30:in `process'
  actionpack (4.2.0) lib/action_controller/metal.rb:195:in `dispatch'
  actionpack (4.2.0) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
  actionpack (4.2.0) lib/action_controller/metal.rb:236:in `block in action'
  actionpack (4.2.0) lib/action_dispatch/routing/route_set.rb:73:in `dispatch'
  actionpack (4.2.0) lib/action_dispatch/routing/route_set.rb:42:in `serve'
  actionpack (4.2.0) lib/action_dispatch/journey/router.rb:43:in `block in serve'
  actionpack (4.2.0) lib/action_dispatch/journey/router.rb:30:in `serve'
  actionpack (4.2.0) lib/action_dispatch/routing/route_set.rb:802:in `call'
  warden (1.2.4) lib/warden/manager.rb:35:in `block in call'
  warden (1.2.4) lib/warden/manager.rb:34:in `call'
  rack (1.6.4) lib/rack/etag.rb:24:in `call'
  rack (1.6.4) lib/rack/conditionalget.rb:38:in `call'
  rack (1.6.4) lib/rack/head.rb:13:in `call'
  actionpack (4.2.0) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
  actionpack (4.2.0) lib/action_dispatch/middleware/flash.rb:260:in `call'
  rack (1.6.4) lib/rack/session/abstract/id.rb:225:in `context'
  rack (1.6.4) lib/rack/session/abstract/id.rb:220:in `call'
  actionpack (4.2.0) lib/action_dispatch/middleware/cookies.rb:560:in `call'
  activerecord (4.2.0) lib/active_record/query_cache.rb:36:in `call'
  activerecord (4.2.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:647:in `call'
  activerecord (4.2.0) lib/active_record/migration.rb:378:in `call'
  actionpack (4.2.0) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
  activesupport (4.2.0) lib/active_support/callbacks.rb:88:in `_run_callbacks'
  activesupport (4.2.0) lib/active_support/callbacks.rb:734:in `_run_call_callbacks'
  activesupport (4.2.0) lib/active_support/callbacks.rb:81:in `run_callbacks'
  actionpack (4.2.0) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
  actionpack (4.2.0) lib/action_dispatch/middleware/reloader.rb:73:in `call'
  actionpack (4.2.0) lib/action_dispatch/middleware/remote_ip.rb:78:in `call'
  better_errors (2.1.1) lib/better_errors/middleware.rb:84:in `protected_app_call'
  better_errors (2.1.1) lib/better_errors/middleware.rb:79:in `better_errors_call'
  better_errors (2.1.1) lib/better_errors/middleware.rb:57:in `call'
  actionpack (4.2.0) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
  web-console (2.3.0) lib/web_console/middleware.rb:28:in `block in call'
  web-console (2.3.0) lib/web_console/middleware.rb:18:in `call'
  actionpack (4.2.0) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  railties (4.2.0) lib/rails/rack/logger.rb:38:in `call_app'
  railties (4.2.0) lib/rails/rack/logger.rb:20:in `block in call'
  activesupport (4.2.0) lib/active_support/tagged_logging.rb:68:in `block in tagged'
  activesupport (4.2.0) lib/active_support/tagged_logging.rb:26:in `tagged'
  activesupport (4.2.0) lib/active_support/tagged_logging.rb:68:in `tagged'
  railties (4.2.0) lib/rails/rack/logger.rb:20:in `call'
  quiet_assets (1.1.0) lib/quiet_assets.rb:27:in `call_with_quiet_assets'
  actionpack (4.2.0) lib/action_dispatch/middleware/request_id.rb:21:in `call'
  rack (1.6.4) lib/rack/methodoverride.rb:22:in `call'
  rack (1.6.4) lib/rack/runtime.rb:18:in `call'
  activesupport (4.2.0) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
  rack (1.6.4) lib/rack/lock.rb:17:in `call'
  actionpack (4.2.0) lib/action_dispatch/middleware/static.rb:113:in `call'
  rack (1.6.4) lib/rack/sendfile.rb:113:in `call'
  railties (4.2.0) lib/rails/engine.rb:518:in `call'
  railties (4.2.0) lib/rails/application.rb:164:in `call'
  rack (1.6.4) lib/rack/content_length.rb:15:in `call'
  thin (1.5.1) lib/thin/connection.rb:81:in `block in pre_process'
  thin (1.5.1) lib/thin/connection.rb:79:in `pre_process'
  thin (1.5.1) lib/thin/connection.rb:54:in `process'
  thin (1.5.1) lib/thin/connection.rb:39:in `receive_data'
  eventmachine (1.0.9.1) lib/eventmachine.rb:193:in `run'
  thin (1.5.1) lib/thin/backends/base.rb:63:in `start'
  thin (1.5.1) lib/thin/server.rb:159:in `start'
  rack (1.6.4) lib/rack/handler/thin.rb:19:in `run'
  rack (1.6.4) lib/rack/server.rb:286:in `start'
  railties (4.2.0) lib/rails/commands/server.rb:80:in `start'
  railties (4.2.0) lib/rails/commands/commands_tasks.rb:80:in `block in server'
  railties (4.2.0) lib/rails/commands/commands_tasks.rb:75:in `server'
  railties (4.2.0) lib/rails/commands/commands_tasks.rb:39:in `run_command!'
  railties (4.2.0) lib/rails/commands.rb:17:in `<top (required)>'
  bin/rails:9:in `<top (required)>'
  spring (1.7.1) lib/spring/client/rails.rb:28:in `call'
  spring (1.7.1) lib/spring/client/command.rb:7:in `call'
  spring (1.7.1) lib/spring/client.rb:30:in `run'
  spring (1.7.1) bin/spring:49:in `<top (required)>'
  spring (1.7.1) lib/spring/binstub.rb:11:in `<top (required)>'
  /Users/ARTLoe/.rvm/rubies/ruby-2.3.0/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
  bin/spring:13:in `<top (required)>'
  bin/rails:3:in `<main>'

내가 어디로 잘못 가고 있는지 조언 해 줄 수 있습니까?

내 사용자 및 기본 설정 모델에는 다음이 있습니다.

class User < ActiveRecord::Base
  has_one :preference, dependent: :destroy
  accepts_nested_attributes_for :preference
end

class Preference < ActiveRecord::Base
  validates_presence_of :category_idealethnicity, presence: true, message: "can't be blank"
  validates_presence_of :category_idealreligion, presence: true, message: "can't be blank"

  belongs_to :user
  has_and_belongs_to_many :category_idealethnicity, dependent: :destroy
  has_and_belongs_to_many :category_idealreligion, dependent: :destroy
end

users_controller

class UsersController < ApplicationController
  respond_to :html, :xml, :json
  before_action :authenticate_user!, :set_user, only: [:show, :edit, :update, :destroy]

  def index
    ...
  end

  def show
    ....
  end

  def edit
    ...
  end

  def update
    ...
  end

  private
  def set_user
    @user = User.friendly.find(params[:id])
  end

  def user_params
    params.require(:user).permit(:email, :firstname, preference_attributes: [:town, category_idealreligion_ids: [], category_idealsmoke_ids: []])
  end
end

preferences_controller

class PreferencesController < ApplicationController
  before_action :authenticate_user!, :set_preference, only: [:show, :edit, :update, :destroy]

  def index
    @preferences = Preference.all
  end

  def show
    ...
  end

  def new
    @users = User.random
    @user = current_user
    @preference = @user.build_preference
  end

  def edit
    ...
  end

  def create
    @user = User.friendly.find(params[:user_id])
    @user.preference = Preference.new(preference_params)
    @preference = @user.preference

    respond_to do |format|
      if @preference.save
        format.html { redirect_to @preference, notice: 'Preference was successfully created.' }
        format.json { render :show, status: :created, location: @preference }
      else
        format.html { render :new }
        format.json { render json: @preference.errors, status: :unprocessable_entity }
      end
    end
  end

  def update
    ...
  end

  def destroy
    ...
  end

  private
  def set_preference
    @preference = Preference.find(params[:id])
  end

  def preference_params
    params.require(:preference).permit(:town, :user_id, category_idealethnicity_ids: [], category_idealreligion_ids: [])
  end
end

노선

Rails.application.routes.draw do
  devise_for :users
  resources :users do
    resources :preferences
  end
end

선호 양식

<div>
  <%= simple_form_for [@user, @preference] do |f| %>
    <%= f.error_notification %>
    <%= f.association :category_idealethnicity, collection: CategoryIdealethnicity.all, prompt: "select", label: false, as: :check_boxes %>
    <%= f.association :category_idealreligion, collection: CategoryIdealreligion.all, prompt: "select", label: false, as: :check_boxes %>
    <div class="form-actions">
      <%= f.button :submit %>
    </div>
  <% end %>
</div>
ARTLoe

질문에 대한 답변 : 내 생성 방법 표시가 잘못되었습니다. 아래는 올바른 코드입니다.

def create
    @preference = Preference.new(preference_params)
    @preference.user = current_user

    respond_to do |format|
      if @preference.save
        format.html { redirect_to @preference, notice: 'Preference was successfully created.' }
        format.json { render :show, status: :created, location: @preference }
      else
        format.html { render :new }
        format.json { render json: @preference.errors, status: :unprocessable_entity }
      end
    end
  end

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

AWS CLI ce 명령으로 새 연결을 설정하지 못했습니다.

분류에서Dev

Docker 내에서 elasticsearch 사용 : 새 연결을 설정하지 못했습니다.

분류에서Dev

스프링 부트 새로 설정된 연결의 유효성을 검사하지 못했습니다.

분류에서Dev

Spring JDBC가 Oracle 11g와 새로 설정된 연결을 검증하지 못했습니다.

분류에서Dev

Rails 4- 'has_one'연관이있는 새 중첩 모델을 생성하지 않고 모델을 저장할 수 없습니다.

분류에서Dev

기본 포트를 설정하면 Rabbitmq가 연결을 거부합니다.

분류에서Dev

네트워크 설정을 적용하지 못했습니다.이 기기를 통해 Bluetooth 네트워크에 연결하십시오.

분류에서Dev

연결을 저장하기 전에 연결 정보를 저장하려면 어떻게해야합니까?

분류에서Dev

연결을 저장하기 전에 연결 정보를 저장하려면 어떻게해야합니까?

분류에서Dev

새 연결을 지정하기위한 최신 iptables 구문

분류에서Dev

저장소 정보를 다운로드하지 못했습니다. 인터넷 연결을 확인하십시오.

분류에서Dev

저장소 정보를 다운로드하지 못했습니다. 인터넷 연결을 확인하십시오.

분류에서Dev

200 PORT 명령이 성공했습니다. PASV 사용을 고려하십시오. 425 연결을 설정하지 못했습니다.

분류에서Dev

mount는 새로 연결된 파일 시스템에 대한 기본 마운트 옵션을 어떻게 결정합니까?

분류에서Dev

Bash : 피어에 의해 SSH 연결이 재설정 된 후 기록이 손실됩니다. 기록을 저장하는 방법은 무엇입니까?

분류에서Dev

두 개의 연결된 스피너가 공유 기본 설정으로 저장되지 않음

분류에서Dev

Firefox 기본 프로그램 연결을 설정할 수 없습니다.

분류에서Dev

중첩 된 has_one 연관을 정의하는 방법은 무엇입니까?

분류에서Dev

방화벽을 열지 않고 연결된 "연결된 서버"를 설정할 수 있습니까?

분류에서Dev

모든 것을 새 컴퓨터로 옮겼습니다. 거북이 svn을 설치했습니다. 모든 작업 복사본을 리포지토리에 다시 연결하려면 어떻게해야합니까?

분류에서Dev

번들 설치 결과 : Gem :: Ext :: BuildError : ERROR : Gem 기본 확장을 빌드하지 못했습니다. ... 설치 실패, 종료 코드 2

분류에서Dev

Ubuntu Nautilus의 기본 파일 연결을 연결 프로그램 탭에 나열되지 않은 항목으로 설정하려면 어떻게합니까?

분류에서Dev

Ubuntu Nautilus의 기본 파일 연결을 연결 프로그램 탭에 나열되지 않은 항목으로 설정하려면 어떻게합니까?

분류에서Dev

Excel에서 설정된 외부 데이터 연결을 사용하여 OLAP 큐브를 사용할 수 없지만 동일한 분석 서버로 새 연결을 만들 수 있습니다.

분류에서Dev

내 서버가 일정 기간 내에 연결을받지 못했는지 어떻게 알 수 있습니까?

분류에서Dev

컬을 연결하지 못했습니다.

분류에서Dev

확인란 값을 저장하면 연결된 이미지 표시 / 숨기기 문제가 발생합니다.

분류에서Dev

Python3 소켓 연결을 설정하지 못했습니다 .gaierror : 이름 또는 서비스를 알 수 없습니다.

분류에서Dev

SQL은 기본값으로 플래그가 지정되지 않은 연결된 레코드를 찾습니다

Related 관련 기사

  1. 1

    AWS CLI ce 명령으로 새 연결을 설정하지 못했습니다.

  2. 2

    Docker 내에서 elasticsearch 사용 : 새 연결을 설정하지 못했습니다.

  3. 3

    스프링 부트 새로 설정된 연결의 유효성을 검사하지 못했습니다.

  4. 4

    Spring JDBC가 Oracle 11g와 새로 설정된 연결을 검증하지 못했습니다.

  5. 5

    Rails 4- 'has_one'연관이있는 새 중첩 모델을 생성하지 않고 모델을 저장할 수 없습니다.

  6. 6

    기본 포트를 설정하면 Rabbitmq가 연결을 거부합니다.

  7. 7

    네트워크 설정을 적용하지 못했습니다.이 기기를 통해 Bluetooth 네트워크에 연결하십시오.

  8. 8

    연결을 저장하기 전에 연결 정보를 저장하려면 어떻게해야합니까?

  9. 9

    연결을 저장하기 전에 연결 정보를 저장하려면 어떻게해야합니까?

  10. 10

    새 연결을 지정하기위한 최신 iptables 구문

  11. 11

    저장소 정보를 다운로드하지 못했습니다. 인터넷 연결을 확인하십시오.

  12. 12

    저장소 정보를 다운로드하지 못했습니다. 인터넷 연결을 확인하십시오.

  13. 13

    200 PORT 명령이 성공했습니다. PASV 사용을 고려하십시오. 425 연결을 설정하지 못했습니다.

  14. 14

    mount는 새로 연결된 파일 시스템에 대한 기본 마운트 옵션을 어떻게 결정합니까?

  15. 15

    Bash : 피어에 의해 SSH 연결이 재설정 된 후 기록이 손실됩니다. 기록을 저장하는 방법은 무엇입니까?

  16. 16

    두 개의 연결된 스피너가 공유 기본 설정으로 저장되지 않음

  17. 17

    Firefox 기본 프로그램 연결을 설정할 수 없습니다.

  18. 18

    중첩 된 has_one 연관을 정의하는 방법은 무엇입니까?

  19. 19

    방화벽을 열지 않고 연결된 "연결된 서버"를 설정할 수 있습니까?

  20. 20

    모든 것을 새 컴퓨터로 옮겼습니다. 거북이 svn을 설치했습니다. 모든 작업 복사본을 리포지토리에 다시 연결하려면 어떻게해야합니까?

  21. 21

    번들 설치 결과 : Gem :: Ext :: BuildError : ERROR : Gem 기본 확장을 빌드하지 못했습니다. ... 설치 실패, 종료 코드 2

  22. 22

    Ubuntu Nautilus의 기본 파일 연결을 연결 프로그램 탭에 나열되지 않은 항목으로 설정하려면 어떻게합니까?

  23. 23

    Ubuntu Nautilus의 기본 파일 연결을 연결 프로그램 탭에 나열되지 않은 항목으로 설정하려면 어떻게합니까?

  24. 24

    Excel에서 설정된 외부 데이터 연결을 사용하여 OLAP 큐브를 사용할 수 없지만 동일한 분석 서버로 새 연결을 만들 수 있습니다.

  25. 25

    내 서버가 일정 기간 내에 연결을받지 못했는지 어떻게 알 수 있습니까?

  26. 26

    컬을 연결하지 못했습니다.

  27. 27

    확인란 값을 저장하면 연결된 이미지 표시 / 숨기기 문제가 발생합니다.

  28. 28

    Python3 소켓 연결을 설정하지 못했습니다 .gaierror : 이름 또는 서비스를 알 수 없습니다.

  29. 29

    SQL은 기본값으로 플래그가 지정되지 않은 연결된 레코드를 찾습니다

뜨겁다태그

보관