0

我有这个应用程序,用户可以在其中为学校写评论。用户必须使用 Facebook 登录才能保存评论。

问题:

如果用户未签名并写了评论,然后用 facebook 登录,他们必须再次写相同的评论。

我想更像这样处理它:unsigned user write a review, signs in with facebook and then the review is saved. But right now the user writes a review and then signs in and nothing is saved, the user have to write the review all over again.`

怎么能做到这一点?任何想法或解决方案都会很棒!

审核表:

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

        private 
        def signed_in?
            !current_user.nil?
        end

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

2 回答 2

1

这听起来像是使用匿名用户的好地方。在http://railscasts.com/episodes/393-guest-user-record查看 Ryan Bates 关于访客用户的视频。

于 2013-02-24T21:25:27.673 回答
1

通常的简单方法是在重定向到身份验证服务之前将参数保存到会话中。一旦用户进行身份验证,您将检查这些属性是否存在并创建评论。

于 2013-02-24T22:43:39.333 回答