ruby-on-rails - before_filter:authenticate_user !,除了:[:index]/Rails 4

标签 ruby-on-rails authentication devise ruby-on-rails-4

我有一个Listings Controller(Devise用户系统),在 Rails 3 中,我刚刚使用过

before_filter :authenticate_user!, except: [:index]

在查看特定列表之前检查用户是否已登录。

我的主页(索引)在底部显示一个列表,用户可以看到它们,但是,一旦他单击一个 View 以查看它,他就会被重定向到登录页面。

这就是为什么我在 Controller 中没有
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.

我的列表 Controller
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

EDIT: PROBLEM 2



如果另一个“登录用户”尝试查看另一个用户在获取此信息时创建的列表->

和日志

最佳答案

尝试此操作,这将允许 guest 查看参数中提供的列表:

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

更新:

看来您想按参数而不是current_user显示 list 。如果是这样,请如下更新您的set_listing定义:
def set_listing
    @listing = Listing.find(params[:id]) if params[:id]
end

关于ruby-on-rails - before_filter:authenticate_user !,除了:[:index]/Rails 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17911046/

相关文章:

ruby-on-rails - Ruby,Errno::EACCES,权限被拒绝,链轮

ruby-on-rails - 每当 gem 不执行任务时

ruby-on-rails - 表单提交后设计覆盖重定向

ruby-on-rails - 设计的自定义身份验证策略

ruby-on-rails - Date.tomorrow 引用两天后

javascript - 通过Passport Js注册用户

php - 服务器不愿意执行。 PHP 中的 LDAP

node.js - Express res.render 不适用于 Angular 路由

ruby-on-rails - 如何配置设备接受 HTTP header 中的身份验证 token ?

ruby-on-rails - 在登录时设计一个 session 变量