我知道这个问题已被提出很多,但其他解决方案都没有对我有用。我创建了一个名为Properties的脚手架并且没有更改它的任何代码,当我点击自动生成的链接来创建一个新属性时,它会在标题中抛出错误消息,特别是在我的properties_controller中定位我的def set_property。我已经为用户创建了一个完美的默认支架,这就是为什么我很困惑。
我在rails v 5.0.2和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
我的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
我按下以获取消息的链接:
<%= link_to 'New Property', new_property_path %>
新的属性页:
<h1>New Property</h1>
<%= render 'form', property: @property %>
<%= link_to 'Back', properties_path %>
以及此页面呈现的表单:
<%= 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 %>
答案 0 :(得分:0)
从:new
移除before_action :set_property
:
before_action :set_property, only: [:show, :edit, :update, :destroy]
set_property
在id
哈希中搜索params
属性,然后使用属性设置@property
(来自数据库的记录)与id
匹配,但new
您不想搜索现有的属性,您正在创建一个新的属性。这就是为什么new
方法将@property
设置为Property.new
:
# GET /properties/new
def new
@property = Property.new
end