Home noMethodError in Property#new
Reply: 0

noMethodError in Property#new

user2792
1#
user2792 Published in April 22, 2018, 8:03 pm

I am having a bit of an issue to find where this error is coming from.

I am getting this error:

I am not sure, it the issue has to do with the naming of my folder in views, I named it property ... instead of properties (I already tried changing it but I still get an error)

This is what my Model looks like

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

and this is my controller:

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

and my view file

_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' %>

It seems that the error is with the form, because if I comment the form out, the new.html.erb displays fine. Any help would be appreciated it.

rake routes | grep property

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.338645 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO