manis
manis

Reputation: 729

form doesn't save foreign key

I am trying to create a Product, which has a foreign key to a Category model, but the foreign key is never saved to the database for some reason.

This is my form

<%= nested_form_for @product do |f| %>
<div>
    <%= f.label :category %><br>
    <%= f.select :category_id, Category.all.map{ |s| [s.name, s.id]}, :include_blank => true %>
</div>
<div>
    <%= f.label :name %><br>
    <%= f.text_field :name %><br>
</div>
<div>
    <%= f.label :price %><br>
    <%= f.number_field :price %><br>
</div>
<div>
    <%= f.label :description %><br>
    <%= f.text_area :description %><br>
</div>

    <%= f.submit %>
<% end %>

and controller

class Admin::ProductsController < AdminController

  def index
    @products = Product.all
  end

  def new
    @product = Product.new
  end

  def create
    @product = Product.new(product_params)
    if @product.save
        flash[:notice] = "You have added #{@product.name}"
        redirect_to admin_products_path
    else
        flash[:error] = "An error occured"
        render "new" 
    end     
  end

  def edit
    @product = Product.find(params[:id])
  end

  def update
    @product = Product.find(params[:id])
    if @product.update(product_params)
      flash[:notice] = "Succesfully updated #{@product[:name].titleize}"
      redirect_to admin_products_path
    else
      flash[:error] = "An error occred update #{@product[:name].titleize}"
      render "edit"
    end
  end

  def destroy
    @product = Product.find(params[:id])
    if @product.destroy
      flash[:notice] = "You succesfully removed #{@product.name}"      
    else 
      flash[:error] = "An error occured trying to remove #{@product.name}"
    end
    redirect_to admin_products_path
  end

  private

    def product_params
            params.require(:product).permit(:name, :price, :category_id, :description,
        stocks_attributes: [ :id, :size, :amount, :_destroy ])
        end

end

Server log:

Processing by ProductsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"+rHOeT8GvAB4zF5QjR1RLJiFyh38d+bgwgA4KS6Fiz8=", "product"=>{"category_id"=>"1", "name"=>"awfsdsd", "price"=>"111", "description"=>"adasdada"}, "commit"=>"Create Product"}
  Admin Load (0.3ms)  SELECT  "admins".* FROM "admins"  WHERE "admins"."id" = 1  ORDER BY "admins"."id" ASC LIMIT 1
Unpermitted parameters: category_id
   (0.1ms)  BEGIN
   (0.2ms)  ROLLBACK
Completed 422 Unprocessable Entity in 30ms

ActiveRecord::RecordInvalid - Validation failed: Category can't be blank:

Upvotes: 0

Views: 226

Answers (1)

Kirti Thorat
Kirti Thorat

Reputation: 53018

It seems that you have two controllers, one named ProductsController and other named Admin::ProductsController. If you notice the error log, you would see that the request is going to ProductsController#create and NOT Admin::ProductsController#create.

You have permitted category_id in Admin::ProductsController but not in ProductsController

Update the form as below:

<%= nested_form_for [:admin, @product] do |f| %>

So, upon form submission it is directed to correct controller i.e., Admin::ProductsController.

UPDATE

but I only have a "show" action in ProductsController

Update the routes for ProductsController as below:

resources :products, only: [:show] 

instead of

resources :products

Upvotes: 1

Related Questions