jeudi 28 janvier 2016

Rails 4 ActiveRecord::AssociationTypeMismatch in JobsController#create

In trying to create a new job for a boat and get the ActiveRecord::AssociationTypeMismatch error. The specific error message is "User(#70281252898540) expected, got Fixnum(#70281243046520)" I also tried to create a job via the rails console, and it rolled back the transaction. Can anyone help debug my code, please?

class BoatsController < ApplicationController

    def new 

        @boat = Boat.new
  end

 def create

        @boat = Boat.create(boat_params)
        if @boat.save
            redirect_to @boat
        else 
            render :new
        end
  end

 def show

        @boat = Boat.find(params[:id])
        @job = Job.new
 end    

 private

    def boat_params
        params.require(:boat).permit(:name, :max_container, :location, :user_id, :avatar).merge(user_id: current_user)
    end
 end

 class JobsController < ApplicationController

    def create

       @job = Job.new(job_params)
       redirect_to @job.boat
    end

private

    def job_params
        params.require(:job).permit(:name, :cost, :description, :origin, :destination).merge(user: current_user, boat_id: params[:boat_id])
    end
 end

 User.rb

  class User < ActiveRecord::Base

    has_many :boats
    has_many :jobs
 end

 Boat.rb

   class Boat < ActiveRecord::Base
    belongs_to :user
    has_many :jobs

 has_attached_file :avatar, :styles =>
    { :medium => "300x300>", :thumb => "100x100>" },
    :default_url => "/images/:style/missing.png"
    validates_attachment_content_type :avatar,
    :content_type => /\Aimage\/.*\Z/
        validates :name, uniqueness: true
        validates :location, inclusion: { in: %w(New\ York Florida Russia     England Ireland Norway Singapore New\ Jersey), message: "%{value} is not a valid location"}
 end

Job.rb

  class Job < ActiveRecord::Base
    belongs_to :user
    belongs_to :boat

 validates :name, uniqueness: true
    validates_numericality_of :cost, :greater_than =>
    1000, :only_integer => true, :allow_nil => true, :message => "Must be
    greater than 1000"
    validates_length_of :description, minimum: 50
    validates :origin, :destination, inclusion: { in: %w(New\ York Florida Russia England Ireland Norway Singapore New\ Jersey), message: "%{value} is not a valid location"}
 end

Schema

  create_table "boats", force: :cascade do |t|
    t.string   "name"
    t.string   "max_container"
    t.string   "location"
    t.integer  "user_id"
    t.datetime "created_at",          null: false
    t.datetime "updated_at",          null: false
    t.string   "avatar_file_name"
    t.string   "avatar_content_type"
    t.integer  "avatar_file_size"
    t.datetime "avatar_updated_at"
 end

 add_index "boats", ["user_id"], name: "index_boats_on_user_id"
  create_table "jobs", force: :cascade do |t|
    t.string   "name"
    t.decimal  "cost"
    t.string   "description"
    t.string   "origin"
    t.string   "destination"
    t.integer  "user_id"
    t.integer  "boat_id"
    t.datetime "created_at",  null: false
    t.datetime "updated_at",  null: false
 end

 add_index "jobs", ["boat_id"], name: "index_jobs_on_boat_id"
   add_index "jobs", ["user_id"], name: "index_jobs_on_user_id"
   create_table "users", force: :cascade do |t|
    t.string   "name"
    t.string   "username"
    t.string   "password_digest"
    t.datetime "created_at",          null: false
    t.datetime "updated_at",          null: false
    t.string   "avatar_file_name"
    t.string   "avatar_content_type"
    t.integer  "avatar_file_size"
    t.datetime "avatar_updated_at"
 end

 Routes.rb
  resources :users
  resources :boats do
    resources :jobs , shallow: true, only: [:create, :edit, :destroy]
 end

 Boats/show.html.erb

  <ul>
    <li><p>Boat Name: <%= @boat.name %></p></li>
    <li><p>Max Container Amount: <%= @boat.max_container %></p></li>
    <li><p>Boat location: <%= @boat.location %></p></li>
 </ul>

 <%= image_tag @boat.avatar.url %>

 <h4>Add Jobs here!</h4>
   <%= form_for [@boat, @job] do |f| %>
   <%= f.text_area :name, placeholder: "Job Name" %>
   <%= f.text_area :cost, placeholder: "Job Cost" %>
   <%= f.text_area :description, placeholder: "Job Description" %>
   <%= f.text_area :origin, placeholder: "Job Origin" %>
   <%= f.text_area :destination, placeholder: "Job Destination" %>
   <%= f.submit "create job" %>
 <% end %>

 <% @boat.jobs.each do |f| %>
    <p><%= job.name %> (<%= job.user.name %>)</p>
 <% end %>

Aucun commentaire:

Enregistrer un commentaire