我遇到了一些問題以找到此錯誤來自哪裏。屬性#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