Rails:未定义的方法`model_name'为零:NilClass

时间:2015-05-30 05:16:51

标签: jquery ruby-on-rails ruby

如何创建单独的页面,同时仍能从控制器和模型中获取用户信息?

例如,我想拥有单独的页面,但仍然使用产品控制器中的相同信息。

视图/产品/的 order.html.erb

然而,当我添加simple_form_for时,我收到错误

<%= simple_form_for(@products) do |f| %>

基本上我试图拥有的是一个提交产品信息的separte页面。如果不是,我很乐意在同一页面内进行操作并拥有像这样的旋转simple_form,而无需创建多个页面。

http://malsup.com/jquery/cycle/

示例:

Example-1 Example-2 Example-3

products_controller.rb

class ProductsController < ApplicationController
 before_action :set_product, only: [:show, :edit, :update, :destroy]

def index
@products = Product.where(availability: true)
  respond_with(@products)
end


def show
end


def new
  @product = Product.new
end


def edit
  authorize! :manage, @product
end


def create
    @product = current_user.products.new(product_params)
  @product.save
  respond_with(@product)
end


def update
    authorize! :manage, @product
  respond_to do |format|
    if @product.update(product_params)
      format.html { redirect_to @product, notice: 'Product was successfully updated.' }
      format.json { render :show, status: :ok, location: @product }
    else
      format.html { render :edit }
      format.json { render json: @product.errors, status: :unprocessable_entity }
    end
  end
end

def destroy
    authorize! :manage, @product
  @product.destroy
  respond_to do |format|
    format.html { redirect_to products_url, notice: 'Product was successfully destroyed.' }
    format.json { head :no_content }
  end
end

1 个答案:

答案 0 :(得分:1)

<%= simple_form_for(@product) do |f| %>

使用@product代替@products

将以下内容添加到products_controller.rb

def order
  @product = Product.new
end