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