Home noMethodError in Property#new
Reply: 1

noMethodError in Property#new

Lucky500
1#
Lucky500 Published in 2017-12-03 02:46:08Z

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

XML Slayer
2#
XML Slayer Reply to 2017-12-03 03:35:57Z

If you declare a form_for without explicitly defining the url, Rails will guess what route you are wanting. In your case, since @property is a new instance (unpersisted), Rails wants to POST to the properties_path. This is simply Rails convention.

Your most likely solution is just to add resources :properties to your routes.rb file. If you paste your file we can give you a bit more information as to why this is necessary.

UPDATE

Rails expects table names (and therefore routes) to be the pluralized version of your model name. So your Property model has a properties table and route. Using resources :properties (plural) follows Rails conventions and let's everything work together nicely.

You need to login account before you can post.

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

© 2016 Powered by mzan.com design MATCHINFO