2017-12-03 166 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

相關問題