Ruby on rails ActionView::Template::错误:未定义nil:NilClass的方法“StoreTitle”
更新版-我已经采纳了最初的建议,谢谢!但我还是有同样的问题。我已经更新了下面的所有内容 我有两种型号,属于一家商店的产品 我正在尝试在属于某个存储的产品的两个视图中显示相关对象的列Store.name,这些产品似乎无法正确保存该存储。请注意:对这一点和学习仍然非常陌生 产品型号: 产品表格 产品视图show.html.erb: 你会注意到我有。在我引用product.store中的product.store.name的地方尝试一下,这样我就不会在本文的主题中列出错误 当我使用控制台查找我正在查看的产品时,我看到了Store_id:2 当我查找id为2的store时,我看到store_id:1-因此存在一个值 我在show视图上打印了params,只得到以下内容:{action=>show,controller=>products,id=>2}Ruby on rails ActionView::Template::错误:未定义nil:NilClass的方法“StoreTitle”,ruby-on-rails,ruby,ruby-on-rails-4,rails-models,Ruby On Rails,Ruby,Ruby On Rails 4,Rails Models,更新版-我已经采纳了最初的建议,谢谢!但我还是有同样的问题。我已经更新了下面的所有内容 我有两种型号,属于一家商店的产品 我正在尝试在属于某个存储的产品的两个视图中显示相关对象的列Store.name,这些产品似乎无法正确保存该存储。请注意:对这一点和学习仍然非常陌生 产品型号: 产品表格 产品视图show.html.erb: 你会注意到我有。在我引用product.store中的product.store.name的地方尝试一下,这样我就不会在本文的主题中列出错误 当我使用控制台查找我正在查看
任何人都能找到我在整个设置中缺少的东西,以便在我的产品视图中显示product.store.name吗?让我知道,如果我可以提供更多的信息 您为数据库中的字段取了一个非常规的名称。默认情况下,活动记录在product表中查找store_id字段。表中的字段名应该是snake-case。因此,现在您必须在产品模型中明确说明存储模型的外键。首先,您应该使用snake-case作为属性。在Ruby中,大写是为常量名保留的,它包括类和模块等内容。更新代码,使属性(例如ProdDesc、ProdImageUrl)不使用大写样式命名。此外,属性也不需要使用诸如Prod*之类的前缀 所以不是
class Product < ActiveRecord::Base
belongs_to :store
validates_presence_of :ProdTitle, :ProdUrl, :ProdPrice
end
您的类将如下所示:
class Product < ActiveRecord::Base
belongs_to :store
validates_presence_of :title, :url, :price
end
您可以在此处阅读有关Ruby和Rails命名约定的更多信息:
我怀疑这是问题的根源。您还需要重命名数据库中的列,并更改控制器和视图代码以反映更改。如果可以,请重新开始您的应用程序。谢谢,我已修复了答案。好的,很高兴知道!更新到snake_case&now:ProdTitle为:name。获取@product.store.name的值时仍然存在相同的错误。现在我的产品看起来像这样:
# GET /products
# GET /products.json
def index
@products = Product.all
end
# GET /products/1
# GET /products/1.json
def show
end
# GET /products/new
def new
@product = Product.new
@stores = Store.all
end
# GET /products/1/edit
def edit
end
# POST /products
# POST /products.json
def create
@product = Product.new(product_params)
respond_to do |format|
if @product.save
format.html { redirect_to @product, notice: 'Product was successfully created.' }
format.json { render action: 'show', status: :created, location: @product }
else
format.html { render action: 'new' }
format.json { render json: @product.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /products/1
# PATCH/PUT /products/1.json
def update
respond_to do |format|
if @product.update(product_params)
format.html { redirect_to @product, notice: 'Product was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @product.errors, status: :unprocessable_entity }
end
end
end
# DELETE /products/1
# DELETE /products/1.json
def destroy
@product.destroy
respond_to do |format|
format.html { redirect_to products_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_product
@product = Product.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def product_params
params.require(:product).permit(:name, :description, :imageurl, :url, :price, :Store_id)
end
end
<%= form_for(@product) do |f| %>
<% if @product.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@product.errors.count, "error") %> prohibited this product from being saved:</h2>
<ul>
<% @product.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="form-group">
<%= f.label :name, "Name" %>
<%= f.text_field :name, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :description, "Description" %>
<%= f.text_area :description, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :imageurl, "Image" %>
<%= f.text_field :imageurl, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :url, "Web Address" %>
<%= f.text_field :url, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :price, "Price" %>
$<%= f.text_field :price, class: "form-control" %>
</div>
<div class="form-group">
<%= collection_select(:product, :Store_id, Store.all, :id, :name, {:prompt=> "Select A Store"}, {:class => "form-control"} ) %>
</div>
<div class="form-group">
<%= f.submit class: "btn btn-primary" %>
</div>
<% end %>
<%= params.inspect %>
<p id="notice"><%= notice %></p>
<p>
<strong>Name:</strong>
<%= @product.name %>
</p>
<p>
<strong>Description:</strong>
<%= @product.description %>
</p>
<p>
<strong>Store Id:</strong>
<%= @product.Store_id %>
</p>
<p>
<strong>Store Name:</strong>
<%= @product.store.try(:name) %>
</p>
<p>
<strong>Image:</strong>
<%= @product.imageurl %>
</p>
<p>
<strong>Url:</strong>
<%= @product.url %>
</p>
<p>
<strong>Price:</strong>
$<%= @product.price %>
</p>
<%= params.inspect %>
<%= link_to 'Edit', edit_product_path(@product) %> |
<%= link_to 'Back', products_path %>
class Product < ActiveRecord::Base
belongs_to :store
validates_presence_of :ProdTitle, :ProdUrl, :ProdPrice
end
class Product < ActiveRecord::Base
belongs_to :store
validates_presence_of :title, :url, :price
end