gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 11:54:36 26 4
gpt4 key购买 nike

我有一个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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com