ruby on rails - AssociationTypeMismatch in ItemsController#create -


im creating online retail store. has items belongs_to category. when try submit item category selected wont save

i tried many hours fix can't figure out. see problem is?

error

activerecord::associationtypemismatch in itemscontroller#create category(#70298375791060) expected, got string(#70298372605800)    def create     @item = current_user.items.build(item_params)     if @item.save       redirect_to @item       flash[:success] = "you have created new item" 

items form

<h1>create new item</h1>  <div class="container">   <div class=“row”>     <div class="col-md-6 col-md-offset-3">       <div class="panel panel-primary">         <div class="panel-body">           <%= simple_form_for @item, html: { multipart: true } |f| %>             <%= f.input :image%>             <%= f.collection_select :category, category.order(:name), :id, :name, include_blank: true, :prompt => "select 1 category" %>             <%= f.input :title%>             <%= f.input :price %>             <%= f.input :description %>             <%= f.button :submit, "create new item", class: "btn btn-primary" %>           <% end %>         </div>       </div>         </div>   </div> </div> 

items controller

class itemscontroller < applicationcontroller   before_action :correct_user_edit,   only: [:edit, :update, :destroy]    def index     @item = @user.items.paginate(page: params[:page])   end    def new     @item = item.new   end    def home     @items = item.paginate(page: params[:page])   end    def edit     @item = item.find(params[:id])     @user = user.find(params[:id])   end    def show     @item = item.find(params[:id])   end    def update     @item = item.find(params[:id])     if @item.update(item_params)        redirect_to @item        flash[:success] = 'item updated.'     else       render "edit"     end   end    def create     @item = current_user.items.build(item_params)     if @item.save       redirect_to @item       flash[:success] = "you have created new item"     else       flash[:danger] = "your item didn't save"       render "new"     end   end    def destroy     item.find(params[:id]).destroy     flash[:success] = "item deleted"     redirect_to users_url   end    private      def item_params       params.require(:item).permit(:title, :category, :price, :description, :image)     end      #check see if user can edit item.     def correct_user_edit       if @item = current_user.items.find_by(id: params[:id])       else         flash[:danger] = "you can't edit item"         redirect_to root_url if @item.nil?       end     end  end 

item model

class item < activerecord::base     belongs_to :user     belongs_to :category     validates :category, presence: true     validates :title, presence: true, length: { maximum: 30 }      validates :price, presence: true     validates :description, presence: true, length: { maximum: 2000 }     validates :user_id, presence: true     has_attached_file :image, styles: { large: "600x600", medium: "250x250", thumb:"100x100#"}     validates_attachment_content_type :image, content_type: /\aimage\/.*\z/ end 

category model

class category < activerecord::base     has_ancestry     has_many :items end 

activerecord::associationtypemismatch in itemscontroller#create category(#70298375791060) expected, got string(#70298372605800)

this line

<%= f.collection_select :category, category.order(:name), :id, :name, include_blank: true, :prompt => "select 1 category" %> 

should be

<%= f.collection_select :category_id, category.order(:name), :id, :name, include_blank: true, :prompt => "select 1 category" %> 

and change in the item_params well

def item_params   params.require(:item).permit(:title, :category_id, :price, :description, :image) end 

undefined method `category_id' item:0x007fdf43bebed0

you should have category_id column in items table. run following command creates migration file adding category_id items.

rails g migration add_category_id_to_items category_id:integer 

and run rake db:migrate

and suggest read these guides before going further.


Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -