samedi 5 août 2017

Rollback transaction on pressing create button and fetching the value field to insert it into the form_for

ERROR

Started POST "/products" for 127.0.0.1 at 2017-08-05 01:23:20 -0700

Processing by ProductsController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"WcfuzGz2ZaEpFmagKYTm3feGTaZxNFPlTkLu/epw7fWObs+pdO4McXw9cLUNjTguav0i97rJR1sLhL5Fk+mk0g==", "product_attribute"=>{"name"=>"RAM 2355 Ghz", "size"=>"4GB", "description"=>"Its a very gooooooooood Ram"}, "value"=>"1", "commit"=>"Create"}

(0.1ms) begin transaction

(0.1ms) rollback transaction

Rendering products/new.html.erb within layouts/application

Rendered products/new.html.erb within layouts/application (2.2ms)

Completed 200 OK in 68ms (Views: 63.6ms | ActiveRecord: 0.1ms)

Description

I'm getting the above error whenever i hit the create button after filling the form.

Products Controller

class ProductsController < ApplicationController

def new
    @product = ProductAttribute.new
    @value = params[:value]
end

def create
    @product = ProductAttribute.new(product_params)
    if @product.save
        redirect_to statics_url
    else
        render 'new'
    end
end


private

def product_params
    params.require(:product_attribute).permit(:name,:value,:size,:description)
end

end

statics Controller

class StaticsController < ApplicationController

def index
    @products = Product.all
end

def new
    @product = Product.new
end

def show
    @product = Product.find(params[:id])
    @attributes = ProductAttribute.where(value: @product.value)
end

def create
    @product = Product.new(product_params)
    if @product.save
        redirect_to root_url
    else
        render 'new'
    end
end

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

def update
    @product = Product.find(params[:id])
    if @product.update(product_params)
        redirect_to root_url
    else
        render 'edit'
    end
end

private

    def product_params
        params.require(:product).permit(:name,:value)
    end 

end

Static view show.html.erb

<h1>Product listing now</h1>

<% @attributes.each do |attribute| %>
    <li><%= attribute.name%></li>
    <li><%= attribute.value%></li>
    <li><%= attribute.size%></li>
    <li><%= attribute.description%></li>
<% end %>

<%= link_to "Create New Product Attributes", new_product_path(value: 
@product.value) %>

Static view new.html.erb

<h1> New Product Creation </h1>

<%= form_for(@product, url: statics_path) do |f| %>
    <%= f.label :name %>
    <%= f.text_field :name, class: 'form-control' %>

    <%= f.label :value %>
    <%= f.text_field :value, class: 'form-control' %>

    <%= f.submit "Create", class: "btn btn-primary" %>
<% end %>

Product view new.html.erb

<h1>Add the New Product Attribute</h1>

<%= form_for(@product, url: products_path) do |f|%>
    <%= f.label :name %>
    <%= f.text_field :name, class: 'form-control' %>

    <%= hidden_field_tag :value, @value %>

    <%= f.label :size %>
    <%= f.text_field :size, class: 'form-control' %>

    <%= f.label :description %>
    <%= f.text_field :description, class: 'form-control' %>

    <%= f.submit "Create", class: 'btn btn-primary' %>
<% end %>

DESCRIPTION

what i'm trying to do is that, i am passing the value attribute through new_static_path(value: @product.value). I am trying to create a new Product attribute field using the existing value field(which is the primary key). Such as example: Ram(parent field) -> (many child field with common value attributes). And i'm using the hidden_field_tag in the product's view new.html.erb so that it will be derived from controller.(i'm confused about this).

Please help me out with this, i'm a bit new to rails. Any help appreciated, thanks.

Aucun commentaire:

Enregistrer un commentaire