通过登录持久化表单输入 [英] Persisting form input over login

查看:42
本文介绍了通过登录持久化表单输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have this app where a user can write a review for a school. A user must sign in with Facebook to save a review. The problem is if a user is unsigned and writes a review, then signs in with Facebook they have to write the same review again.

我正在尝试通过在会话中存储评论数据表单来解决此问题,但我无法使其正常工作.

I am trying to fix this by storing the review data form in sessions, but I cant quite make it work.

执行此操作的正确 rails 方法是什么?

What is the proper rails way to do this?

审核表格:

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

推荐答案

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

You should save the review in the create sessions action (which is not included in your question). Assuming you are using omniauth, you can add something on the action that handles the callback

# 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

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

I used delete so the session will be cleared of these values.

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

UPDATE: since you're using a session controller

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

这篇关于通过登录持久化表单输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆