ActionView :: Template :: Error:未定义的方法`StoreTitle'代表nil:NilClass

时间:2013-12-21 02:06:50

标签: ruby-on-rails ruby ruby-on-rails-4 rails-models

更新版本 - 我已经提供了初步建议(谢谢!)但我仍然遇到同样的问题。我已经更新了以下所有内容。

我有两个型号,属于商店的产品。

我试图在属于商店的产品的几个视图中显示相关对象的列(Store.name),但似乎无法使商店正确保存。请注意:这还是一个非常新的和学习。

产品型号:

    class Product < ActiveRecord::Base

        belongs_to :store
        validates_presence_of :name, :url, :price


    end

商店模型:

class Store < ActiveRecord::Base

has_many :products
has_many :pins, through: :products
accepts_nested_attributes_for :products

end

产品控制器:

class ProductsController&lt; ApplicationController的       before_action:set_product,only:[:show,:edit,:update,:destroy]

  # 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 %>

查看产品(show.html.erb):

<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 %>

你会注意到我有.try在我从product.store引用product.store.name的地方,所以我不再收到这篇文章主题中列出的错误。

当我查看我正在使用控制台查看的产品时,我看到Store_id:2

当我查找id为2的商店时,我看到Store_id:1 - 所以那里有一个值。

我在节目视图上打印了参数并且只能得到:{“action”=&gt;“show”,“controller”=&gt;“products”,“id”=&gt;“2”}。

任何人都可以找到我在整个设置中缺少的内容,以便在我的产品视图中显示product.store.name吗?如果我能提供更多信息,请告诉我!

2 个答案:

答案 0 :(得分:1)

首先,您应该使用snake_case作为属性。 UpperCamelCase保留给Ruby中的常量名称,包括类和模块之类的东西。更新代码以不对属性使用UpperCamelCase样式命名(例如,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

您的课程将如下所示:

{{1}}

您可以在此处阅读有关Ruby和Rails命名约定的更多信息:

我怀疑这会导致问题。您还需要重命名数据库中的列,并更改控制器和查看代码以反映更改。如果可以,请重新开始申请。

答案 1 :(得分:0)

您已在字段数据库中取名为非常规字段。默认情况下,活动记录在产品表中查找store_id字段。表中的字段名称应为蛇形。现在您必须在产品型号中明确告知商店模型的外键

相关问题