2013-07-28 5 views
19

मैं यदि कोई उपयोगकर्ता में हस्ताक्षर किए गए एक Listings Controller (वसीयत उपयोगकर्ता सिस्टम) और में रेल 3 मैं सिर्फ इस्तेमाल कियाbefore_filter: authenticate_user !, को छोड़कर: [: सूचकांक]/रेल 4

before_filter :authenticate_user!, except: [:index] 

जाँच करने के लिए है एक विशिष्ट लिस्टिंग देखने से पहले।

मेरा होमपेज (इंडेक्स) नीचे एक दृश्य सूची में दिखाता है, उपयोगकर्ता उन्हें देख पाता है, लेकिन जैसे ही वह इसे देखने के लिए क्लिक करता है, उसे लॉगिन पेज पर रीडायरेक्ट किया जाता है।

क्यों मेरे नियंत्रक में मैं

Listing.new -> current_user.listings.new 

के बजाय था रेल 4 बातों में है बदल गया है लगता है और मैं नहीं कर सकते यह करने के लिए सही रास्ता खोजने।

मैं थोड़ा खोज की है और पाया गया कि आदेश

before_action :authenticate_user!, :except => [:index] 

एक अतिथि में बदल गया था अब सूचकांक देख सकते हैं, लेकिन अगर वह एक लिस्टिंग पर क्लिक करता है, वह बजाय मैं प्रवेश पृष्ठ पर पुनः निर्देशित नहीं है, यह त्रुटि प्राप्त करें।

NoMethodError in ListingsController#show 
undefined method `listings' for nil:NilClass 

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

# Never trust parameters from the scary internet, only allow the white list through. 

मेरे लिस्टिंग नियंत्रक

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

    # GET /listings 
    # GET /listings.json 
    def index 
    @listings = Listing.order("created_at desc") 
    end 

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

    # GET /listings/new 
    def new 
     @listing = current_user.listings.build 
    end 

    # GET /listings/1/edit 
    def edit 
    end 

    # POST /listings 
    # POST /listings.json 
    def create 
     @listing = current_user.listings.build(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 = current_user.listings.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, :image) 
    end 
end 

संपादित करें: समस्या 2

एक और उपयोगकर्ता में लॉग इन एक और उपयोगकर्ता द्वारा बनाए गए जो im प्रविष्टि को देखने की कोशिश करता है हो रही इस ->

enter image description here

और लॉग

enter image description here

+2

यू करना चाह सकते हैं की तरह नहीं है 'set_listing' – Santhosh

+0

से पहले' authenticate_user' करें, क्या आप केवल set_listing लाइन के ऊपर प्रमाणीकरण_user को ले जाने का मतलब है? –

+0

हां। क्या तुमने कोशिश की? – Santhosh

उत्तर

7

इस प्रयास करें, इस मेहमानों लिस्टिंग पैरामीटर में आपूर्ति को देखने के लिए अनुमति देगा:

def set_listing 
    unless current_user 
     @listing = Listing.find(params[:id]) 
    else 
     @listing = current_user.listings.find(params[:id]) 
    end 
end 

अद्यतन:

ऐसा प्रतीत होता है कि आप पैरामीटर द्वारा लिस्टिंग प्रदर्शित करना चाहते हैं और current_user द्वारा नहीं।तो फिर अपने set_listing परिभाषा अपडेट कर लें इस प्रकार है:

def set_listing 
    @listing = Listing.find(params[:id]) if params[:id] 
end 
+0

मुझे नियो की मदद से पहले कार्रवाई की गई है। लेकिन अब अगर मैं किसी अन्य खाते में लॉग इन करता हूं, और एक सूची देखने की कोशिश करता हूं जो मेरे द्वारा नहीं बनाई गई है, तो मुझे "NoMethodError में ListingsController # शून्य के लिए अपरिभाषित विधि 'लिस्टिंग' दिखाएं: NilClass"। आपके कोड के साथ :( –

+0

@TheMiniJohn, क्या आप अपने लॉग से ट्रेस पोस्ट कर सकते हैं। – vee

+0

मैंने प्रश्न संपादित किया। –

25

कॉल authenticate_userset_listing से पहले, ताकि current_user नहीं है nil

before_action :authenticate_user!, :except => [:index] 
before_action :set_listing, only: [:show, :edit, :update, :destroy] 
+0

काम किया, धन्यवाद :) –

-3

हाँ आप, set_listing से पहले authenticate_user कॉल करने के लिए की जरूरत है ताकि current_usernil

before_action :authenticate_user!, :except => [:index] 
before_action :set_listing, only: [:show, :edit, :update, :destroy] 

इस

+0

यह उत्तर मेरा से अलग कैसे है? – Santhosh

संबंधित मुद्दे