我在下面的代码中遇到了两个问题,
我正尝试做@products.each do |product|
,但它让我退缩了undefined nill class on product
-我已经检查了产品控制器中的问题,但仍未定义。
当我尝试从下面的简单表单创建新产品,但它似乎没有保存到数据库中时,是因为设计问题吗? (它在url链接上显示了对令牌进行身份验证等),这就是它在url中显示的内容:
How to replace text in Google Spreadsheet using App Scripts?
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
答案 0 :(得分:0)
这是导致问题的原因
def show
@user = User.new
end
从用户模型和用户控制器中删除
并在产品控制器中
def update
@product = Product.find(params[:id])
if @product.update(product_params)
redirect_to product_path(@cocktail)
else
render :edit
end
end
在哪里获得实例变量@coctail
,它应该是@product
并且由于您不允许产品控制器中的条件,我想您不需要
<%= f.input :condition,
required: true,
autofocus: true,
input_html: { autocomplete: "condition" }%>
并更新
<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>
使用
<div id='container'>
<div class='signup_create_product'>
<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 %>
</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>
答案 1 :(得分:0)
这里发生了几件事,但是请仔细检查,关于“未定义nil类”的错误很可能与实例变量@projects
有关,而不是与迭代变量project
有关。也就是说,@projects
未定义(或您尝试定义它,并且未返回任何实际记录)。例如,如果您尚未创建任何项目,显然Project.all
将返回nil
,因此each
不会有任何迭代。
尝试使用byebug
来检查控制器中用于索引操作的情况。
关于您的第二个问题,这里不应该讨论Devise,因为Project并不是Devise模型。但是,您上面包含的代码在顶部附近有一条虚假的<form>
行-这很可能与您稍后创建的form_tag
冲突。