gpt4 book ai didi

ruby-on-rails - Rails - before_filter 未定义局部变量

转载 作者:数据小太阳 更新时间:2023-10-29 08:00:40 24 4
gpt4 key购买 nike

我有这个表单,用户可以在其中输入评论。用户必须在 Facebook 上签名才能保存评论。我使用 before_filter 检查用户是否登录。

但我得到这个错误:undefined local variable or method signed_in_user'.

另一件事是,我如何使用 Facebook 登录用户并保存其评论?不会丢失并让用户再次输入相同的评论。

审核表:

<%= form_for [@school, Review.new] do |f| %>
<%= f.text_area :content %>
<% if current_user %>
<%= f.submit 'Save my review', :class => "btn" %>
<% else %>
<%= f.submit 'Save my review and sign me into facebook', :class => "btn" %>
<% end %>
<%end %>

评论 Controller

class ReviewsController < ApplicationController
before_filter :signed_in_user, only: [:create, :destroy]

def create
@school = School.find(params[:school_id])
@review = @school.reviews.new(params[:review])

@review.user_id = current_user.id

if @review.save
redirect_to @review.school, notice: "Review has been created."
else
render :new
end
end

def new
@school = School.find_by_id(params[:school_id])
@review = Review.new
end
end

ReviewsHelper

module ReviewsHelper
def signed_in?
!current_user.nil?
end

def signed_in_user
unless signed_in?
redirect_to "/auth/facebook"
end
end
end

我正在使用 omniauth 对来自 facebook 的用户进行身份验证。

最佳答案

在 Controller 中包含您的ReviewsHelper:

class ReviewsController < ApplicationController
include ReviewsHelper #or helper :reviews
before_filter :signed_in_user, only: [:create, :destroy]

def create
@school = School.find(params[:school_id])
@review = @school.reviews.new(params[:review])

@review.user_id = current_user.id

if @review.save
redirect_to @review.school, notice: "Review has been created."
else
render :new
end
end

def new
@school = School.find_by_id(params[:school_id])
@review = Review.new
end
end

关于ruby-on-rails - Rails - before_filter 未定义局部变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15054683/

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