2017-04-25 40 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

啊謝謝堆,工作完美!我對軌道非常陌生,所以現在有點壓倒性了。 –

相關問題