Reputation: 13
I know this question has been asked a lot but none of the other solutions worked for me. I have created a scaffold called Properties and have not changed any of the code for it, when I click on the automatically generated link to create a new property, it throws the error message in the title, specifically targeting my def set_property in my properties_controller. I have already created a default scaffold for Users which worked perfectly, which is why I am very confused.
I am on rails v 5.0.2 and ruby v 2.3.3
My Routes.rb:
Rails.application.routes.draw do
get 'sessions/create'
get 'sessions/destroy'
get 'users/about'
resources :users
resources :properties
get 'admin' => 'admin#index'
controller :sessions do
get 'login' => :new
post 'login' => :create
delete 'logout' => :destroy
end
root 'users#home'
end
My properties_controller.rb
class PropertiesController < ApplicationController
before_action :set_property, only: [:show, :edit, :update, :destroy, :new]
# GET /properties
# GET /properties.json
def index
@properties = Property.all
end
# GET /properties/1
# GET /properties/1.json
def show
end
# GET /properties/new
def new
@property = Property.new
end
# GET /properties/1/edit
def edit
end
# POST /properties
# POST /properties.json
def create
@property = Property.new(property_params)
respond_to do |format|
if @property.save
format.html { redirect_to @property, notice: 'Property was successfully created.' }
format.json { render :show, status: :created, location: @property }
else
format.html { render :new }
format.json { render json: @property.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /properties/1
# PATCH/PUT /properties/1.json
def update
respond_to do |format|
if @property.update(property_params)
format.html { redirect_to @property, notice: 'Property was successfully updated.' }
format.json { render :show, status: :ok, location: @property }
else
format.html { render :edit }
format.json { render json: @property.errors, status: :unprocessable_entity }
end
end
end
# DELETE /properties/1
# DELETE /properties/1.json
def destroy
@property.destroy
respond_to do |format|
format.html { redirect_to properties_url, notice: 'Property was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_property
@property = Property.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def property_params
params.require(:property).permit(:name, :price, :bed, :bath, :car, :inspect)
end
end
The link I press to get the message:
<%= link_to 'New Property', new_property_path %>
New property page:
<h1>New Property</h1>
<%= render 'form', property: @property %>
<%= link_to 'Back', properties_path %>
and the form that this page renders:
<%= form_for(property) do |f| %>
<% if property.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(property.errors.count, "error") %> prohibited this property from being saved:</h2>
<ul>
<% property.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :price %>
<%= f.number_field :price %>
</div>
<div class="field">
<%= f.label :bed %>
<%= f.number_field :bed %>
</div>
<div class="field">
<%= f.label :bath %>
<%= f.number_field :bath %>
</div>
<div class="field">
<%= f.label :car %>
<%= f.number_field :car %>
</div>
<div class="field">
<%= f.label :inspect %>
<%= f.text_field :inspect %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Upvotes: 1
Views: 277
Reputation: 10517
Remove :new
from before_action :set_property
:
before_action :set_property, only: [:show, :edit, :update, :destroy]
set_property
searches for an id
attribute in the params
hash and then sets the @property
with the property (record from the db) that matches that id
, but with new
you don't want to search for an existing property, you are creating a new one. So thats why new
method sets @property
with Property.new
:
# GET /properties/new
def new
@property = Property.new
end
Upvotes: 1