2013-07-27 3 views
0

사용자가 Listings Controller인데 사용자가 목록을 작성할 수 있습니다.레일 4 용 컨트롤러의 Current_user

난 그냥

Listing to current_user.listings 

에서 모든 행동을 업데이트 한 다른 사용자 목록을 편집 할 사용자를 방지하기 위해하지만, 레일 4 컨트롤러는 변화가 있고 난이를 설정하는 방법을 찾을 수 없습니다.

내 컨트롤러 파일 ->

class ListingsController < ApplicationController 
    before_action :set_listing, only: [:show, :edit, :update, :destroy] 
    before_filter :authenticate_user!, :only => [:index] 

    # GET /listings 
    # GET /listings.json 
    def index 
    @listings = Listing.all 
    end 

    # GET /listings/1 
    # GET /listings/1.json 
    def show 
    end 

    # GET /listings/new 
    def new 
    @listing = Listing.new 
    end 

    # GET /listings/1/edit 
    def edit 
    end 

    # POST /listings 
    # POST /listings.json 
    def create 
    @listing = Listing.new(listing_params) 

    respond_to do |format| 
     if @listing.save 
     format.html { redirect_to @listing, notice: 'Listing was successfully created.' } 
     format.json { render action: 'show', status: :created, location: @listing } 
     else 
     format.html { render action: 'new' } 
     format.json { render json: @listing.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # PATCH/PUT /listings/1 
    # PATCH/PUT /listings/1.json 
    def update 
    respond_to do |format| 
     if @listing.update(listing_params) 
     format.html { redirect_to @listing, notice: 'Listing was successfully updated.' } 
     format.json { head :no_content } 
     else 
     format.html { render action: 'edit' } 
     format.json { render json: @listing.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # DELETE /listings/1 
    # DELETE /listings/1.json 
    def destroy 
    @listing.destroy 
    respond_to do |format| 
     format.html { redirect_to listings_url } 
     format.json { head :no_content } 
    end 
    end 

    private 
    # Use callbacks to share common setup or constraints between actions. 
    def set_listing 
     @listing = Listing.find(params[:id]) 
    end 

    # Never trust parameters from the scary internet, only allow the white list through. 
    def listing_params 
     params.require(:listing).permit(:title, :description) 
    end 
end 

사람은 해결책을 알고있다?

답변

3

에서 #new로 변경하십시오. 그래서, 모든 @listing = Listing.new에 변경 :

@listing = current_user.listings.build 

를 그런 다음에 변화를 set_listing에 :

@listing = current_user.listings.find(params[:id]) 
+0

을 당신은 하나님입니다! 고맙습니다. –

+0

실제로 다른 사용자가 Listings-> ActiveRecord :: RecordNotFound를 보려고하면이 문제가 생깁니다. id = 13 인 목록을 찾을 수 없습니다. [WHERE "listings". "user_id"=?] –

+0

그 점이 중요한가요? 어떻게이 기능을 원하니? –