0

我有这个应用程序,用户可以在其中为学校写评论。用户必须使用 Facebook 登录才能保存评论。问题是,如果用户未签名并撰写评论,然后使用 Facebook 登录,他们必须再次撰写相同的评论。

我试图通过将评论数据表单存储在会话中来解决这个问题,但我无法让它发挥作用。

什么是正确的 rails 方法来做到这一点?

审核表:

<%= 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 %>

审查控制器

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

    def save_review(school, review, rating)
        Review.create(:content => review, :school_id => school, 
                       :user_id => current_user, :rating => rating)
    end

    private 
    def signed_in?
        !current_user.nil?
    end

    def signed_in_user
        unless signed_in?
            # Save review data into sessions
            session[:school] = School.find(params[:school_id])
            session[:review] = params[:review]
            session[:rating] = params[:rating] 
            # Login the user to facebook
            redirect_to "/auth/facebook"
            # After login save review data for user
            save_review(session[:school], session[:review], session[:rating])
        end
    end
end
4

2 回答 2

3

我的理解是,除了像用户令牌等非常小的东西之外,在会话中存储东西不是“Rails 方式”。你可以在 Obie Fernandez 的 The Rails 3 Way 中阅读更多关于这个想法的信息。

我建议您从一开始就将评论存储在数据库中,并且仅在评论连接到 Facebook 身份验证用户后“显示”评论。如果您对如何实现这一点有任何好奇,我很乐意详细说明。

编辑:这里有一些示例代码。首先,我会负责将用户与评论相关联,以实现“永久”存储。您可以将 a 添加user_idreview表中,但这可能是null大多数时候,这对我来说似乎很草率:

$ rails g model UserReview review_id:references, user_id:references

然后我会用 a和 a创建一个user_session_review表。这是用于“临时”存储:review_iduser_session_token

$ rails g model UserSessionReview review_id:integer, user_session_token:string

然后,当用户注册时,将任何“临时”评论与该用户相关联:

class User
  has_many :user_reviews
  has_many :reviews, through: :user_reviews
  has_many :user_session_reviews

  def associate_reviews_from_token(user_session_token)
    temp_reviews = UserSessionReview.find_all_by_user_session_token(user_session_token)
    temp_reviews.each do |temp_review|
      user_reviews.create!(review_id: temp_review.review_id)
      temp_review.destroy
    end
  end
end

所以在你的控制器中,你可能会这样做

class UsersController < ApplicationController
  def create
    # some stuff
    @user.associate_reviews_from_token(cookies[:user_session_token])
  end
end

当然,您必须在字里行间稍加阅读,但我认为这应该可以让您继续前进。

编辑2:要删除旧的废弃评论,我会这样做:

class UserSessionReview
  scope :old, -> { where('created_at < ?', Time.zone.now - 1.month) }
end

然后,在 cron 作业中:

UserSessionReview.old.destroy_all
于 2013-02-27T20:06:16.930 回答
2

您应该将评论保存在创建会话操作中(不包含在您的问题中)。假设您使用的是omniauth,您可以在处理回调的操作上添加一些内容

# review controller
def signed_in_user
  unless signed_in?
    # Save review data into sessions
    session[:school] = School.find(params[:school_id])
    session[:review] = params[:review]
    session[:rating] = params[:rating]

    # Login the user to facebook
    redirect_to "/auth/facebook"   
  end
end

# callback to login the user
def handle_callback
  # do your thing here to login the user
  # once you have the user logged in
  if signed_in?
    if session[:school] && session[:review] && session[:rating] # or just 1 check
      Review.create(
        content: session.delete(:review),
        school_id: session.delete(:school), 
        user_id: current_user.id,
        rating: session.delete(:rating)
      )
      #redirect_to somewhere
    end
  end
end

我使用了删除,因此会话将清除这些值。

更新:因为您使用的是会话控制器

class SessionsController < ApplicationController
  def create
    if user = User.from_omniauth(env["omniauth.auth"])
      session[:user_id] = user.id 

      if session[:school] && session[:review] && session[:rating] # or just 1 check
        review = Review.new
        review.content = session.delete(:review)
        review.school_id = session.delete(:school)
        review.user_id = user.id
        review.rating = session.delete(:rating)
        review.save
      end
    end

    redirect_to :back
  end
于 2013-02-28T09:09:17.170 回答