我一直在葡萄酒中获得NoMethodError #new - 有谁知道问题是什么?

时间:2015-12-04 16:29:53

标签: ruby-on-rails

所以我只有一个模型(葡萄酒模型)和一个索引页面,显示所有当前的葡萄酒和我有多少瓶。我已经建立了所有观点,我只想跟踪葡萄酒的名称和葡萄酒的数量。但是,当我点击“创建一个新酒”并且我的_form.html.erb文件中第2行出现问题时,我不断收到Wines#new中的NoMethodError错误。但我找不到问题所在。

以下是我的_form.html.erb文件:

 <%= form_for @wine do |form| %>
 <div>Name: <%= form.text_field :name %></div></br>
 <div>Number: <%= form.text_field :number %></div></br>
 <%= form.submit %>
 <% end %>

我的index.html.erb文件:

<h1>VIEWING ALL WINES</h1>
<!-- INDEX -->

<div class="buttons"> <%= link_to '<button type="button">Create a New      Wine</button>'.html_safe, new_wine_path %></div>
<div class="container">
<h1><% @wines.each do |item| %></h1>
<div class="text">
  <div>
    <h3>NAME: <a href="wines/<%= item['id'] %>"><%= item['name'] %></a></h3>
  </div>
  <div>
    <p><B>NUMBER OF BOTTLES: </b><%= item['number'] %></p>
  </div>
</div>

My Wines_Controller.rb文件:

class WinesController < ApplicationController

# INDEX --------------------------
  @wines = Wine.all
#  @wines = "wines index working"
end

# NEW -------------------------
 def new
 #  render text: "new working"
 @wine = Wine.new
 end

# CREATE --------------------------

def create
#  render text: "create working"
@wine = Wine.create(wine_params)
redirect_to wines_path
end

# EDIT --------------------------
def edit
#  render text: "edit working"
@wine = Wine.find(params[:id])
end

# UPDATE --------------------------
def update
#  render text: "update working"
@wine = Wine.find(params[:id])
@wine.update_attributes(wine_params)
redirect_to wines_path
end

# SHOW --------------------------
def show
#  render text: "show working"
@wine = Wine.find(params[:id])
end

# DESTROY --------------------------
def destroy
#  render text: "destroy working"
@nwine = Wine.find(params[:id])
@wine.destroy
redirect_to wines_path
end

# STRONG PARAMS --------------------
def wine_params
    params.require(:wine).permit(:name, :number)
end

结束

2 个答案:

答案 0 :(得分:0)

不确定为什么你在链接里面使用按钮,你可以在里面使用button_to,就像这样。而且按钮通常使用post方法,而不是像link_to这样的方法,你可以做这样的事情。

<%= button_to "Create a New Wine", new_wine_path, class: "button", method: :get %>

答案 1 :(得分:0)

错误是说没有create方法,这是真的。改变这个:

@wine = Wine.create(wine_params)

这个

@wine = Wine.new(wine_params)
if @wine.save
    redirect_to root_url
end

另外,我建议你把

def wine_params
    params.require(:wine).permit(:name, :number)
end

进入private区块,就像这样

private

def wine_params
    params.require(:wine).permit(:name, :number)
end

我猜你是想在这一行创建一个按钮

<%= link_to '<button type="button">Create a New Wine </button>'.html_safe, new_wine_path %></div>

为什么不坚持一个简单的link_to?像这样:

<%= link_to 'Create a new wine', new_wine_path %>