jeudi 23 mai 2019

undefined method create product/ for nil class

There're two problems I encountered in the codes below, 1. I was trying to do @products.each do |product| but it threw me back undefined nill class on product - I've checked the problem in product controller but it's still not defined. 2. when I was trying to create a new product from the simple form for below but it doesn't seem to be saved into the db, is it because the devise issues?(it shows on the url link that authenticate token etc)this is what it shown in the url:http://localhost:3000/products/new?utf8=%E2%9C%93&authenticity_token=7L5t8CunwqhCGwaCs1L5BXG7WAFBxLE0kbpoDojXeE%2FzzUOPAXVZqXnFRw29OLPD2SsJ%2F%2FHm2EkT0q1qUvxYdQ%3D%3D&product%5Bproduct_name%5D=fvf&product%5Bcondition%5D=dfvd&product%5Bdescription%5D=vdfvdc&commit=Submit

class Product < ApplicationRecord
  belongs_to:user
  validates :product_name, presence: true, uniqueness: true

end

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable

  has_many:products
    validates :name, presence: true, uniqueness: true

  def show

    @user = User.new

  end

end


class UsersController < ApplicationController



  def show

    @user = User.new

  end

end


  <div id='container'>
  <div class='signup_create_product'>
     <form>

<h1>Create a new product</h1>
      <%= simple_form_for @product do |f| %>
       <%= f.input :product_name,
                required: true,
                autofocus: true,
                input_html: { autocomplete: "product_name" }%>

   <%= f.input :condition,
                required: true,
                autofocus: true,
                input_html: { autocomplete: "condition" }%>

    <%= f.input :description,
                required: true,
                autofocus: true,
                input_html: { autocomplete: "description" }%>

         <div class="form-actions">
                <%= f.button :submit, "Submit" %>
              </div>
            <% end %>

     </form>
  </div>
  <div class='whysign'>
    <h1>Share your beloved products with all over the world!</h1>
    <p>This is an Airbnb style platform that you can create your own products and sell it or purchase from all over the world!</p>

    <i class="fas fa-home"></i><%= link_to 'home',root_path  %>


</div>

  </div>

class ApplicationController < ActionController::Base
  # [...]
  before_action :configure_permitted_parameters, if: :devise_controller?

  def configure_permitted_parameters
    # For additional fields in app/views/devise/registrations/new.html.erb
    devise_parameter_sanitizer.permit(:sign_up, keys: [:first_name, :last_name])

    # For additional in app/views/devise/registrations/edit.html.erb
    devise_parameter_sanitizer.permit(:account_update, keys: [:username])
  end
end

class ProductsController < ApplicationController


  def index
    @products = Product.all
  end

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

  def new
    @product = Product.new
  end

  def create
    @product = Product.new(product_params)

    if @product.save
      redirect_to product_path(@product)
    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 product_path(@cocktail)
    else
      render :edit
    end
  end

  def destroy
    @product = Product.find(params[:id])
    @product.destroy
    redirect_to products_path
  end

  private

  def product_params
    params.require(:product).permit(:product_name, :description)
  end
end



Aucun commentaire:

Enregistrer un commentaire