【问题标题】:NoMethodError in Products#create产品中的 NoMethodError#create
【发布时间】:2013-11-03 17:21:51
【问题描述】:

我对编程完全陌生,但遇到了麻烦。大约 10 天前,我在 Richard Schneeman 主持的 ureddit.com 上开始了 UT-Rails 课程。到目前为止,一切进展顺利,但我在第 5 周遇到了麻烦。如果我没有使用正确的术语,您将不得不原谅我,因为它需要接受很多。

https://github.com/zkay/move_logic_to_controllers 是我目前正在学习的教程。

我已完成第 2 步。我已将 app/views/products/new.html.erb 中的文本替换为以下内容:

<%= form_for(@product) do |f| %>
  <div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </div>
  <div class="field">
    <%= f.label :price %><br />
    <%= f.text_field :price %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

但是,当我尝试按照教程添加新产品时,我收到的拒绝是:

NoMethodError in Products#create

Showing C:/Sites/move_logic_to_controllers/app/views/products/create.html.erb where line #3 raised:

undefined method `name' for nil:NilClass
Extracted source (around line #3):

1: <h2>Product Created Successfully<h2>
2: 
3: <%= @product.name %> added to the website, it costs: $<%= @product.price %>
Rails.root: C:/Sites/move_logic_to_controllers

如果我删除 .name.price 调用,页面可以正常工作,但不会显示我提交的任何数据。

app/controllers/product_controller.rb 我有以下内容:

class ProductsController < ApplicationController
  def index
    @products = Product.includes(:user).all
  end
def new
  @product = Product.new
end

  respond_to do |format|
    if @product.save
      format.html { render :action => "create" }
      format.json { render :json => @product }
    else
      format.html { render :action => "new" }
      format.json { render :json => @product.errors, :status => :unprocessable_entity }
    end
  end
end

对不起,如果这是冗长的。感谢您的帮助。

【问题讨论】:

  • 您的控制器中的create 操作在哪里?
  • /facepalm.我刚刚添加了 def create @product = Product.new(params[:product]) end,但它仍然无法正常工作。
  • 等等,我刚刚保存了我打开的所有文件,它有效吗?但是我在创建视图中将 更改回产品并且它起作用了。有人能解释一下为什么会这样吗?
  • 好..它因为您正在创建一个具有其他名称的对象并使用其他名称访问它..如果这解决了您的问题,而不是将答案标记为已选择...
  • 啊,我想我现在明白了。所以在视图中,我引用了@products,但在控制器中,我引用了@product?

标签: ruby-on-rails


【解决方案1】:

应该是&lt;%= @products.name %&gt;

【讨论】:

  • 你在什么观点上叫这个..??
  • /app/views/products/create.html.erb
【解决方案2】:

/app/views/products/create.html.erb

您不想使用 create.html.erb。

class ProductsController < ApplicationController
  def index
    @products = Product.includes(:user).all
  end

  def new
    @product = Product.new
  end

  def create
    @product = Product.new(params[:product])

    if @product.save
      redirect_to products_path, notice: "You added product"
    else
      flash[:error] = "Something wrong!"
      render :new
    end
  end
end

如果您使用 Rails 4,请使用:

def create
  @product = Product.new(product_params)

  if @product.save
    redirect_to products_path, notice: "You added product"
  else
    flash[:error] = "Something wrong!"
    render :new
  end
end

private

def product_params
  params.require(:product).permit(:name, :price)
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-20
    相关资源
    最近更新 更多