记录用户,然后保存审阅 - rails

时间:2013-02-24 21:22:43

标签: ruby-on-rails ruby ruby-on-rails-3

我有这个应用程序,用户可以为学校写评论。用户必须使用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。`

怎么办呢?任何想法或解决方案都会很棒!

ReviewForm:

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

ReviewsController:

    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

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

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