2017-12-03 35 views
0

このエラーがどこから来たのかを見つけるには少し問題があります。プロパティのnoMethodError#new

私はこのエラーを取得しています

:私が代わりにプロパティの...それをプロパティの名前

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はうまく表示されます。どんな助けもそれに感謝します。

レーキルート| grepのプロパティ

enter image description here

+0

あなたは 'rake routes | grepプロパティ '? – rony36

+0

確かに、私は上の画像を追加しました。なぜなら、答えが長すぎたからです。 – Lucky500

答えて

1

明示的urlを定義せずにform_forを宣言すると、Railsはあなたが望んでいるものの経路を推測します。あなたのケースでは、@propertyが新しいインスタンス(unpersisted)であるため、POSTからproperties_pathにRailsが欲しいです。これは単にRailsの規約です。

最も可能性のある解決策は、ファイルにresources :propertiesを追加することです。あなたがファイルを貼り付けると、これがなぜ必要なのかについてもう少し詳しく知ることができます。

UPDATE

Railsのテーブル名(したがってルートが)あなたのモデル名の複数形バージョンであることを期待しています。したがって、Propertyモデルのテーブルとルートはpropertiesです。 resources :properties(複数形)を使用すると、Railsの規則に従い、すべてがうまく機能するようにします。

+0

ねえXML、私はそれが私のroutes.rbをRails.application.routes.drawに設定されていない devise_forん:ユーザー リソースは: ルートを歓迎 リソース「#インデックス歓迎」:プロパティ エンド – Lucky500

+0

変更 'リソースを:プロパティ 'を' resources:properties'に変更します。 –

+0

これはまったく新しいセットのエラーを与えるようです... [GET] "/ property/new"と一致するルートはありません...私はリンクを変更する必要があると仮定しています何ではない。私はそれを試みるかもしれない。 – Lucky500

関連する問題