Ruby on rails ActiveRecord::RecordNotFound in PropertiesController#new-Cand';找不到具有';id'=
我知道这个问题已经被问了很多次了,但是其他的解决方案都不适合我。我已经创建了一个名为Properties的scaffold,并且没有更改它的任何代码。当我单击自动生成的链接创建新属性时,它会在标题中抛出错误消息,特别是针对我的Properties\u控制器中的def set\u属性。我已经为用户创建了一个工作完美的默认脚手架,这就是为什么我非常困惑的原因 我使用的是rails V5.0.2和ruby V2.3.3 My Routes.rb:Ruby on rails ActiveRecord::RecordNotFound in PropertiesController#new-Cand';找不到具有';id'=,ruby-on-rails,ruby,activerecord,Ruby On Rails,Ruby,Activerecord,我知道这个问题已经被问了很多次了,但是其他的解决方案都不适合我。我已经创建了一个名为Properties的scaffold,并且没有更改它的任何代码。当我单击自动生成的链接创建新属性时,它会在标题中抛出错误消息,特别是针对我的Properties\u控制器中的def set\u属性。我已经为用户创建了一个工作完美的默认脚手架,这就是为什么我非常困惑的原因 我使用的是rails V5.0.2和ruby V2.3.3 My Routes.rb: Rails.application.routes.dr
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
我的属性\u 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 %>
禁止保存此属性:
删除:在操作之前从中新建:设置属性
:
before_action :set_property, only: [:show, :edit, :update, :destroy]
set\u属性
在params
散列中搜索id
属性,然后使用与id
匹配的属性(数据库中的记录)设置@属性
,但是使用new
您不想搜索现有属性,您正在创建一个新属性。这就是为什么new
方法将@property
设置为属性。new
:
# GET /properties/new
def new
@property = Property.new
end
在操作之前从中删除:新建:设置属性
:
before_action :set_property, only: [:show, :edit, :update, :destroy]
set\u属性
在params
散列中搜索id
属性,然后使用与id
匹配的属性(数据库中的记录)设置@属性
,但是使用new
您不想搜索现有属性,您正在创建一个新属性。这就是为什么new
方法将@property
设置为属性。new
:
# GET /properties/new
def new
@property = Property.new
end
然后我得到:ActiveRecord::PropertiesController中的DangerousAttributeError#new-inspect由Active Record定义。检查以确保您没有同名的属性或方法。@NathanMarshall该错误是因为您有一个名为inspect
的属性,并且该单词是由ActiveRecord定义的,即您不能使用它。更改名称,您将很好。啊,谢谢堆,工作完美!我是rails的新手,所以现在一切都有点让人不知所措。然后我得到:ActiveRecord::PropertiesController中的DangerousAttributeError#new-inspect由ActiveRecord定义。检查以确保您没有同名的属性或方法。@NathanMarshall该错误是因为您有一个名为inspect
的属性,并且该单词是由ActiveRecord定义的,即您不能使用它。更改名称,您将很好。啊,谢谢堆,工作完美!我是rails的新手,所以现在一切都有点势不可挡。欢迎!请给出一个简短的代码示例,说明您的尝试。欢迎访问链接!请给出一个简短的代码示例,说明您的尝试。访问链接