【问题标题】:ActiveRecord::AssociationTypeMismatch In Nested Route嵌套路由中的 ActiveRecord::AssociationTypeMismatch
【发布时间】:2016-03-18 16:37:34
【问题描述】:

应用详情:Rails 4 Ruby 2

我有一个嵌套关系,即 Stalls 和 Reservations。

当我尝试从摊位创建新预订时,我收到以下错误:

ActiveRecord::AssociationTypeMismatch

Stall(#70127041150600) expected, got Reservation(#70127025144120)
@stall = Stall.find(params[:stall_id])
@reservation = Reservation.new(reservation_params)

** @reservation.stall = @reservation ** --> Highlighted Red in the error Message

respond_to do |format|
  if @reservation.save

突出显示的代码是在应用程序错误消息中以红色突出显示的代码。

我的模特是:

class Stall < ActiveRecord::Base
  belongs_to :property
  has_many :reservations
end

class Reservation < ActiveRecord::Base
  belongs_to :stall
  belongs_to :user
end

Reservation Controller 中的创建操作:

class Stalls::ReservationsController < ApplicationController
    def create
        @stall = Stall.find(params[:stall_id])
        @reservation = Reservation.new(reservation_params)
        @reservation.stall = @reservation

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

该关系的 routes.rb 设置:

  resources :stalls do
    resources :reservations, controller: 'stalls/reservations'
  end

这里的任何帮助将不胜感激。

提前致谢。

编辑 #1:为 Stalls 添加完整控制器:Reservations

class Stalls::ReservationsController < ApplicationController
  before_action :set_reservation, only: [:show, :edit, :update, :destroy]

  # GET /reservations
  # GET /reservations.json
  def index
    @reservations = Reservation.all
  end

  # GET /reservations/1
  # GET /reservations/1.json
  def show

  end

  # GET /reservations/new
  def new
    @stall = Stall.find(params[:stall_id])
    @reservation = Reservation.new
    @reservation.stall = @reservations
  end

  # GET /reservations/1/edit
  def edit
  end

  # POST /reservations
  # POST /reservations.json
  def create
    @stall = Stall.find(params[:stall_id])
    @reservation = Reservation.new(reservation_params)
    @reservation.stall = @reservation

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

  # PATCH/PUT /reservations/1
  # PATCH/PUT /reservations/1.json
  def update
    respond_to do |format|
      if @reservation.update(reservation_params)
        format.html { redirect_to @reservation, notice: 'Reservation was successfully updated.' }
        format.json { render :show, status: :ok, location: @reservation }
      else
        format.html { render :edit }
        format.json { render json: @reservation.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /reservations/1
  # DELETE /reservations/1.json
  def destroy
    @reservation.destroy
    respond_to do |format|
      format.html { redirect_to reservations_url, notice: 'Reservation was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_reservation
      @reservation = Reservation.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def reservation_params
      params.require(:reservation).permit(:stall_id, :user_id, :arrival_date, :arrival_time, :departure_date)
    end
end

这是错误页面底部的堆栈:

Request

Parameters:

{"utf8"=>"✓",
 "authenticity_token"=>"mCVcslrmH4mh6/kQieZf8gTmLBqEh5evS+jzwti1azx+UKMuO2v1WeeVWVsSZI4Zo9eunPJ2uHTAFHgrQPOYOA==",
 "reservation"=>{"stall_id"=>"",
 "user_id"=>"",
 "arrival_date(1i)"=>"2015",
 "arrival_date(2i)"=>"12",
 "arrival_date(3i)"=>"12",
 "arrival_time(1i)"=>"2015",
 "arrival_time(2i)"=>"12",
 "arrival_time(3i)"=>"12",
 "arrival_time(4i)"=>"23",
 "arrival_time(5i)"=>"31",
 "departure_date(1i)"=>"2015",
 "departure_date(2i)"=>"12",
 "departure_date(3i)"=>"12"},
 "commit"=>"Create Reservation",
 "stall_id"=>"1"}

LocalHost Server Output:

Started POST "/stalls/1/reservations" for ::1 at 2015-12-12 16:31:25 -0700
Processing by Stalls::ReservationsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"mCVcslrmH4mh6/kQieZf8gTmLBqEh5evS+jzwti1azx+UKMuO2v1WeeVWVsSZI4Zo9eunPJ2uHTAFHgrQPOYOA==", "reservation"=>{"stall_id"=>"", "user_id"=>"", "arrival_date(1i)"=>"2015", "arrival_date(2i)"=>"12", "arrival_date(3i)"=>"12", "arrival_time(1i)"=>"2015", "arrival_time(2i)"=>"12", "arrival_time(3i)"=>"12", "arrival_time(4i)"=>"23", "arrival_time(5i)"=>"31", "departure_date(1i)"=>"2015", "departure_date(2i)"=>"12", "departure_date(3i)"=>"12"}, "commit"=>"Create Reservation", "stall_id"=>"1"}
  Stall Load (0.2ms)  SELECT  "stalls".* FROM "stalls" WHERE "stalls"."id" = $1 LIMIT 1  [["id", 1]]
Completed 500 Internal Server Error in 2ms (ActiveRecord: 0.2ms)

ActiveRecord::AssociationTypeMismatch (Stall(#70127051519860) expected, got Reservation(#70127076366540)):
  app/controllers/stalls/reservations_controller.rb:32:in `create'


  Rendered /Users/TaurenLTD1/.rvm/gems/ruby-2.2.0/gems/actionpack-4.2.3/lib/action_dispatch/middleware/templates/rescues/_source.erb (3.4ms)
  Rendered /Users/TaurenLTD1/.rvm/gems/ruby-2.2.0/gems/actionpack-4.2.3/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (2.0ms)
  Rendered /Users/TaurenLTD1/.rvm/gems/ruby-2.2.0/gems/actionpack-4.2.3/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.0ms)
  Rendered /Users/TaurenLTD1/.rvm/gems/ruby-2.2.0/gems/actionpack-4.2.3/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (50.3ms)
  Rendered /Users/TaurenLTD1/.rvm/gems/ruby-2.2.0/gems/web-console-2.2.1/lib/web_console/templates/_markup.html.erb (0.4ms)
  Rendered /Users/TaurenLTD1/.rvm/gems/ruby-2.2.0/gems/web-console-2.2.1/lib/web_console/templates/_inner_console_markup.html.erb within layouts/inlined_string (0.2ms)
  Rendered /Users/TaurenLTD1/.rvm/gems/ruby-2.2.0/gems/web-console-2.2.1/lib/web_console/templates/_prompt_box_markup.html.erb within layouts/inlined_string (0.3ms)
  Rendered /Users/TaurenLTD1/.rvm/gems/ruby-2.2.0/gems/web-console-2.2.1/lib/web_console/templates/style.css.erb within layouts/inlined_string (0.2ms)
  Rendered /Users/TaurenLTD1/.rvm/gems/ruby-2.2.0/gems/web-console-2.2.1/lib/web_console/templates/console.js.erb within layouts/javascript (36.5ms)
  Rendered /Users/TaurenLTD1/.rvm/gems/ruby-2.2.0/gems/web-console-2.2.1/lib/web_console/templates/main.js.erb within layouts/javascript (0.2ms)
  Rendered /Users/TaurenLTD1/.rvm/gems/ruby-2.2.0/gems/web-console-2.2.1/lib/web_console/templates/error_page.js.erb within layouts/javascript (0.3ms)
  Rendered /Users/TaurenLTD1/.rvm/gems/ruby-2.2.0/gems/web-console-2.2.1/lib/web_console/templates/index.html.erb (81.1ms)

没有路线匹配 [GET] "/stalls/1/reservations"

完整跟踪:

actionpack (4.2.3) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
web-console (2.2.1) lib/web_console/middleware.rb:39:in `call'
actionpack (4.2.3) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.2.3) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.2.3) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.2.3) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.2.3) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.2.3) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.2.3) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.2.3) 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.3) 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.3) lib/action_dispatch/middleware/static.rb:116:in `call'
rack (1.6.4) lib/rack/sendfile.rb:113:in `call'
railties (4.2.3) lib/rails/engine.rb:518:in `call'
railties (4.2.3) lib/rails/application.rb:165:in `call'
rack (1.6.4) lib/rack/lock.rb:17:in `call'
rack (1.6.4) lib/rack/content_length.rb:15:in `call'
rack (1.6.4) lib/rack/handler/webrick.rb:88:in `service'
/Users/TaurenLTD1/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/webrick/httpserver.rb:138:in `service'
/Users/TaurenLTD1/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/webrick/httpserver.rb:94:in `run'
/Users/TaurenLTD1/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/webrick/server.rb:294:in `block in start_thread'

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-4 controller nested-routes


    【解决方案1】:
    Stall(#70127041150600) expected, got Reservation(#70127025144120)
    

    错误信息说明了一切。您正在尝试将 reservation 分配给 stall,因此出现错误。

    变化:

    @reservation.stall = @reservation
    

    收件人:

    @reservation.stall = @stall
    

    而且,这应该可以解决您的问题。

    【讨论】:

    • 所以我之前想过,但是当我使用该 reservation.stall = Stall 时,它给了我一个路由错误未初始化常量 StallsController 感谢您的及时回复,这至少给了我一个新的错误一起工作!!欢呼
    • 你能显示完整的错误跟踪吗?和完整的控制器代码?是的,这是我们需要解决的另一个问题。
    • 您正在尝试使用StallsController 但它可能未定义,因此出现新错误。
    • 绝对。给我几分钟,我会更新问题!谢谢。
    • 在尝试创建预留时,我已使用完整的控制器和服务器输出更新了问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-18
    相关资源
    最近更新 更多