Rails - 尝试从创建操作设置cookie

发布于 2024-12-11 12:52:12 字数 1790 浏览 0 评论 0原文

我正在尝试为未登录表单创建操作的用户设置 cookie。一旦未登录的用户点击提交,他们就会被重定向到登录/注册操作,然后返回到之前所在的页面,并且表单会自动填充他们之前输入的信息。

我已成功添加 cookie 并且它们正在保存,但它们返回一个奇怪的字符串对象。

"{\"1\"=>\"This is a test answer\"}" # 1 is the question_id and 'This is a test answer' is the answer_text

这是我设置 cookie 的创建操作:

def create 
  store_location  

 if current_user.nil?
   cookies[:answer_entry] = { :value => params[:answers] }
   # raise p cookies[:answer_entry].inspect 
   deny_access
 else 
   params[:answers].each do |question_id, answer_text|
     next if answer_text.blank?
     question = Question.find(question_id)
     question.answers.create!(:answer => answer_text, :user_id => current_user )
     raise p question.answer
     redirect_to book_questions_path(@book), :notice => "You have successfully submitted your Answer, please answer more!"
   end
  end 
end

我将 cookie 保存到显示操作中的实例变量(这是表单出现的位置),然后将变量作为表单中的值传递

def show
  @book = Book.find(params[:book_id]) 
  @question = @book.questions.find(params[:id])

  if user_signed_in?
    @answer = cookies[:answer_entry]
  else
   cookies[:answer_entry] = nil 
  end
end 

<%= form_for(:answer, :url => book_question_answers_path(@book, @question)) do |f| %>
  <%= text_area_tag "answers[#{@question.id}]", @answer%>
  <%= f.hidden_field :user_id, :value => @current_user %> 
  <%= submit_tag("Submit") %>
<% end %>

然后, 我在表单字段中自动填充了奇怪的字符串。

我有两个问题:

  1. 我知道这是一个字符串 "{\"1\"=>\"这是一个测试答案\"}" 但有人可以解释为什么里面有一个散列以及为什么双引号被逃走了?
  2. 我只想在表单字段中呈现answer_text(“这是测试答案”),有人可以指出我正确的方向来解决这个问题吗?我是否必须拆分字符串并将其重新分配为哈希,然后调用 :answer_text 参数?

我感谢任何和所有的帮助。

谢谢!

I am trying to set cookies for users that aren't signed in on the create action of a form. Once a non-signed in user hits submit, they are redirected to a sign-in/signup action, where they then return back to the page they were previously on and the form auto populates with their previously entered info.

I have successfully added the cookies and they are saving, but they are returning a weird string object.

"{\"1\"=>\"This is a test answer\"}" # 1 is the question_id and 'This is a test answer' is the answer_text

Here is the create action where I am setting the cookies:

def create 
  store_location  

 if current_user.nil?
   cookies[:answer_entry] = { :value => params[:answers] }
   # raise p cookies[:answer_entry].inspect 
   deny_access
 else 
   params[:answers].each do |question_id, answer_text|
     next if answer_text.blank?
     question = Question.find(question_id)
     question.answers.create!(:answer => answer_text, :user_id => current_user )
     raise p question.answer
     redirect_to book_questions_path(@book), :notice => "You have successfully submitted your Answer, please answer more!"
   end
  end 
end

I then save the cookies to an instance variable in the show action (which is where the form appears) and then pass in the variable as a value in the form:

def show
  @book = Book.find(params[:book_id]) 
  @question = @book.questions.find(params[:id])

  if user_signed_in?
    @answer = cookies[:answer_entry]
  else
   cookies[:answer_entry] = nil 
  end
end 

And

<%= form_for(:answer, :url => book_question_answers_path(@book, @question)) do |f| %>
  <%= text_area_tag "answers[#{@question.id}]", @answer%>
  <%= f.hidden_field :user_id, :value => @current_user %> 
  <%= submit_tag("Submit") %>
<% end %>

And from this I am getting that weird string auto populating in the form field.

I have two questions:

  1. I understand that this is a string "{\"1\"=>\"This is a test answer\"}" But can someone explain why there is a hash within and why the double quotes are being escaped?
  2. I want to render ONLY the answer_text ("this is a test answer") in the form field, can someone point me in the right direction to solve this? Do I have to split up the string and reassign it as a hash and then call the :answer_text params?

I appreciate any and all help.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你是年少的欢喜 2024-12-18 12:52:12

您将哈希值存储在 cookies[:answer_entry] 中,这就是为什么当您检索它并将其存储在 @answer 实例变量中时,您会得到一个哈希值,并且当您在 text_area_tag 中使用它时,它会显示为字符串。

有几种方法可以解决这个

问题 方法一

在创建操作中分配给 cookies[:answer_entry] 如下

cookies[:answer_entry] = params[:answers]

方法二

在显示操作中从 cookies[:answer_entry] 检索值,如下所示

@answer = cookies[:answer_entry][:value]

You are storing a hash in cookies[:answer_entry] that is why when you retrieve it and store it in the @answer instance variable you get back a hash and when you use it in the text_area_tag it shows up as a string.

There are a couple of ways to address this

Way one

In the create action assign to the cookies[:answer_entry] as follows

cookies[:answer_entry] = params[:answers]

Way Two

In show action retrieve the value from cookies[:answer_entry] as follows

@answer = cookies[:answer_entry][:value]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文