ActiveRecord :: AssociationTypeMismatch在嵌套路由中

时间:2015-12-12 23:00:22

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

应用程序详细信息: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
  

保留控制器中的创建操作:

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:为档位添加完整控制器:预订

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]&#34; / stalls / 1 / reservations&#34;

完整跟踪:

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'

1 个答案:

答案 0 :(得分:1)

Stall(#70127041150600) expected, got Reservation(#70127025144120)

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

变化:

@reservation.stall = @reservation

要:

@reservation.stall = @stall

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