问题描述
我如何创建单独的页面,同时仍然能够从控制器和模型中获取用户信息?
How exactly do I create separate pages while still able to user info from controller and model?
例如,我希望有单独的页面,但仍使用来自产品控制器的相同信息.
For example, I want to have separate pages but still using the same info from the products controller.
views/product/ order.html.erb
views/product/order.html.erb
但是,当我添加一个simple_form_for时,我会得到一个错误
Yet when I add a simple_form_for I get an error
<%= simple_form_for(@products) do |f| %>
基本上,我要提供的是一个单独的页面,用于提交产品信息.如果不是这样的话,我很乐意在同一个页面中完成操作,并拥有一个旋转的simple_form,而不必创建多个页面.
Basically all I'm trying to have is a separte page to submit info for the product. If not I'd gladly be happy to do it within the same page and have a revolving simple_form like this without having to create multiple pages.
http://malsup.com/jquery/cycle/
示例:
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
推荐答案
<%= simple_form_for(@product) do |f| %>
使用@product代替@products
Use @product instead of @products
将以下内容添加到products_controller.rb
Add following to products_controller.rb
def order
@product = Product.new
end
这篇关于Rails:nil:NilClass的未定义方法"model_name"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!