属性#new中的noMethodError

我遇到了一些问题以找到此错误来自哪里。属性#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的财产

回答:

如果要声明form_for但没有明确界定url,Rails会猜测你想要什么样的路线。在你的情况下,因为@property是一个新的实例(未加载),Rails希望POSTproperties_path。这只是Rails约定。

您最有可能的解决办法是将resources :properties添加到您的routes.rb文件中。如果你粘贴你的文件,我们可以给你更多的信息,为什么这是必要的。

UPDATE

Rails的预计,表名(因此路由)是您的型号名称的多元化的版本。所以你的Property模型有一个properties表和路线。使用resources :properties(复数)遵循Rails约定,让我们的一切很好地协同工作。

以上是 属性#new中的noMethodError 的全部内容, 来源链接: utcz.com/qa/259283.html

回到顶部