2017-04-25 48 views
1

我知道这个问题已被问了很多,但没有其他解决方案为我工作。我创建了一个名为Properties的脚手架,并没有改变它的任何代码,当我点击自动生成的链接来创建一个新属性时,它会在标题中引发错误消息,特别是针对我的properties_controller中的def set_property。我已经为完美工作的用户创建了一个默认脚手架,这就是为什么我很困惑。在PropertiesController ActiveRecord :: RecordNotFound#新 - 找不到属性与'ID'=

我在轨道上v 5.0.2和Ruby 2.3.3 v

我的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 %> 

和形式,这个页面被渲染:在params哈希

before_action :set_property, only: [:show, :edit, :update, :destroy] 

set_property搜索的id属性和:

<%= 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

欢迎您!把简短的代码例子放在你想要的东西上。并访问链接** [如何问](http://stackoverflow.com/help/mcve)** –

回答

0

before_action :set_property删除:new然后将@property财产(db的记录)与id匹配,但是new您不想搜索现有的属性,您正在创建一个新的。所以,这就是为什么new方法设置@propertyProperty.new

# GET /properties/new 
def new 
    @property = Property.new 
end 
+0

然后我得到:ActiveRecord :: DangerousAttributeError在PropertiesController#新 - 检查由Active Record定义。检查以确保您没有具有相同名称的属性或方法。 –

+0

@NathanMarshall这个错误是因为你有一个名为'inspect'的属性,这个单词是由ActiveRecord定义的,也就是说你不能使用它。改变这个名字,你会没事的。 – Gerry

+0

啊谢谢堆,工作完美!我对轨道非常陌生,所以现在有点压倒性了。 –

相关问题