2017-04-25 8 views
1

この質問は多くの質問がありましたが、私にとっては他の解決策はありませんでした。私はプロパティと呼ばれる足場を作成し、それのためのコードを変更していない、新しいプロパティを作成するために、自動的に生成されたリンクをクリックすると、特に私のproperty_controllerで私のdef set_propertyをターゲットに、私はすでに完璧に機能しているユーザー向けのデフォルトの足場を作成しています。そのため、私は非常に混乱しています。PropertiesControllerのActiveRecord :: RecordNotFound#new - 'id'でプロパティを見つけることができませんでした=

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 

私が押しリンク:

私はレールの午前は、2.3.3

マイroutes.rbを5.0.2とルビーV Vメッセージを受け取る:

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

ようこそ:new方法は@propertyProperty.newとを設定し、なぜこれほどのthats!簡単なコード例を入れてみてください。 ** [How to Ask](http://stackoverflow.com/help/mcve)** –

答えて

0

before_action :set_propertyから:newを削除します。

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

set_property検索paramsハッシュでid属性のとプロパティを使用して@propertyを設定しますidに一致する(レコードはdb)ですが、newの場合は、既存のプロパティを検索したくない場合は、新しいものを作成しています。

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

次に、取得します:PropertiesControllerのActiveRecord :: DangerousAttributeError#new - inspectはアクティブレコードによって定義されます。同じ名前の属性またはメソッドがないことを確認してください。 –

+0

@ NathanMarshallこのエラーは、 'inspect'という名前の属性があり、その単語がActiveRecordによって定義されている、つまり使用できないことが原因です。その名前を変更しても問題ありません。 – Gerry

+0

ああ、ありがとう、完璧に働いた!私はかなり新しいレールですので、今は少し圧倒されています。 –

関連する問題