Sneha Bhamra
Sneha Bhamra

Reputation: 21

rails simple_forms and active_storage images are resulting in controller errors when editing products

I am building a small marketplace app using rails and currently am stuck on trying to get my product edit and create pages to work. I have added image upload capability and have utilised simple forms to add details for the products but any time I try to proceed with editing I get the following error:

The action 'update' could not be found for ProductsController

Meanwhile if I try to create a new product I get a different error:

{"seller":["must exist"]}

Please see my code below:

-products_controller.rb

  def create
    @product = Product.create 
    @product_id = @product.id 
      if @product.save
        render :show, status: :created
      else
        render json: @product.errors, status: :unprocessable_entity
      end

    end
  end


  # PATCH/PUT /products/1 or /products/1.json
  def update 
    @product = Product.update (product_params)
      if @product.save
        render products:id, status: :created
      else
        render json: @product.errors, status: :unprocessable_entity
      end
    end

 def product_params
      params.require(:product).permit(:title, :description, :price, :buyer_id, :seller_id, :category, :image_url)
    end
<%= simple_form_for edit_product_path, url: {action: "update"} do |f| %>
    <h1 class="heading">Edit Product</h1>
    <%= render 'form', product: @product %>
<% end %>

-form.html.erb

<div class="form-inputs">
    <%= f.input :title %>
    <%= f.input :description %>
    <%= f.input :price %>
    <%= f.input :category, collection: ["footwear", "accessories", "menswear", "womenswear"] %>
    <div class="form-group">
      <% if product.picture.attached? %>
        <%= image_tag product.picture, style: "width: 200px; display: block" %>
      <% end %>
      <%= f.file_field :picture %>
    </div>
  </div>

Any help I can get is greatly appreciated.

Upvotes: 1

Views: 260

Answers (1)

Kevin Maze
Kevin Maze

Reputation: 156

    @product = Product.create 
    @product_id = @product.id 
      if @product.save
        render :show, status: :created
      else
        render json: @product.errors, status: :unprocessable_entity
      end

    end <--- extra end
  end

It looks like you have an extra hanging end in the middle of your create action. This would probably explain what's going on with it.

If this doesn't fix the issue, ensure that you have the proper routes defined in routes.rb.

If you keep getting the seller must exist error, Rails 5/6 automatically assumes that a belongs_to association will have a model present to associate to when the record is saved. You can turn this off by adding optional: true to your relationship definition like this:

class Product
   belongs_to :seller, optional: true
end

You can include a seller_id in your SimpleForm on page load and associate it with that as long as you add the appropriate filter in product_params. It might look something like:

<%= f.input :seller_id, :input_html => { :value => @seller.id } %>

Upvotes: 0

Related Questions