2017-12-03 162 views
0

我遇到了一些问题以找到此错误来自哪里。属性#new中的noMethodError

我收到此错误:

enter image description here

我不知道,它的问题是与我的观点文件夹的命名做的,我把它命名为财产...而不是性质(我已经尝试过改变,但我仍然得到一个错误)

enter image description here

这是我的模型是什么样子

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的财产

enter image description here

+0

请问您可以添加'rake routes | grep属性? – rony36

+0

当然,我在上面添加了一张图片,因为这里的答案太长了。 – Lucky500

回答

1

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

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

UPDATE

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

+0

嘿XML,我有它设置在我的routes.rb Rails.application.routes.draw做 devise_for:用户 资源:欢迎 根“欢迎#指数” 资源:物业 结束 – Lucky500

+0

变化'资源:属性'到'资源:属性' –

+0

,似乎给了我一整套新的错误...没有路线匹配[GET]“/ property/new”...我假设我将不得不改变链接和什么不是。我可能会尝试。 – Lucky500

相关问题