22

私はListings Controller(Devise User System)を持っており、Rails 3で使用しました

before_filter :authenticate_user!, except: [:index]

特定のリストを表示する前に、ユーザーがサインインしているかどうかを確認します。

マイ ホームページ (インデックス) の下部に一覧が表示されます。ユーザーは一覧を表示できますが、一覧をクリックして表示するとすぐに、ログイン ページにリダイレクトされます。

それが私のコントローラーで私が代わりに持っていた理由です

Listing.new -> current_user.listings.new

Rails 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

別のログインユーザーが、別のユーザーが作成したリストを表示しようとすると、これが表示されます->

ここに画像の説明を入力

そしてログ

ここに画像の説明を入力

4

4 に答える 4

29

authenticate_userの前に呼び出すset_listingので、そうでcurrent_userはありませんnil

before_action :authenticate_user!, :except => [:index]
before_action :set_listing, only: [:show, :edit, :update, :destroy]
于 2013-07-28T18:08:21.707 に答える
7

これを試してみてください。これにより、ゲストはパラメーターで提供されたリストを見ることができます:

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

アップデート:

ではなく、パラメーターごとにリストを表示したいようですcurrent_userset_listingその場合は、次のように定義を更新してください。

def set_listing
    @listing = Listing.find(params[:id]) if params[:id]
end
于 2013-07-28T18:11:20.423 に答える