我遇到了一些问题以找到此错误来自哪里。属性#new中的noMethodError
我收到此错误:
我不知道,它的问题是与我的观点文件夹的命名做的,我把它命名为财产...而不是性质(我已经尝试过改变,但我仍然得到一个错误)
这是我的模型是什么样子
class Property < ApplicationRecord
validates :address, presence: true, length: {minimum: 10}
validates :price, presence: true
validates :description, presence: true
validates :bedrooms, presence: true
validates :bathrooms, presence: true
validates :type, presence: true
validates :sqft, presence: true
validates :lot, presence: true
validates :year_built, presence: true
end
,这是我的控制器:
property_controller.rb
class PropertyController < ApplicationController
def index
@properties = Property.all
end
def new
@property = Property.new
end
def create
@property = Property.new(property_params)
if @property.save?
flash[:notice] = 'Property was successufully created.'
redirect_to property_path(@property)
else
render :new
end
end
private
def property_params
params.require(:property).permit(:address, :price, :description,
:bedrooms, :bathrooms, :type, :sqft, :lot, :year_built)
end
end
和我的视图文件
_form.html.erb
<% if @property.errors.any? %>
<h3>The following errors prevented the addition of this property.
</h3>
<ul>
<% @property.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
<% end %>
<%= form_for @property do |f| %>
<div class="form-group">
<%= f.text_field :address, required: true, placeholder: "Address",
class: "form-control"%>
</div>
<div class="form-group">
<%= f.text_field :price, required: true, placeholder: "Price",
class: "form-control"%>
</div>
<div class="form-group">
<%= f.text_area :description, required: true, placeholder:
"Description", class: "form-control"%>
</div>
<div class="form-group">
<%= f.button :submit, class: "btn btn-success" %>
</div>
<% end %>
new.html.erb
<h3>Add new Property:</h3>
<%#= render 'form' %>
看来,错误与形式,因为如果我的评论形成,new.html.erb显示正常。任何帮助将不胜感激。
rake routes | grep的财产
请问您可以添加'rake routes | grep属性? – rony36
当然,我在上面添加了一张图片,因为这里的答案太长了。 – Lucky500