如何将文本区域值保存到 Rails 会话中?

发布于 2024-12-06 04:23:14 字数 370 浏览 0 评论 0原文

我有一个带有一个输入字段(文本区域)的表单,当用户提交表单时,我想将文本区域的值保存到会话中。在 Rails 中我该如何做到这一点?

我正在使用 Devise,用户提交表单后我将其发送到的页面是我的 Devise 注册页面。

所以我想我在注册控制器操作中需要类似的东西:

session[:text_entered] = params()

...但是 Devise 没有给我一个注册控制器。我需要做一个吗?

提交表单时,我是否遇到了超长 URL 的问题?这应该是 POST 还是 GET?如何将 textarea 值传递到注册页面而不将其作为 URL 参数发送?

抱歉我是新手。感谢您的帮助!

I have a form with one input field -- a textarea -- and when the user submits the form I want to save the value of the textarea to the session. How do I do that, in Rails?

I'm using Devise, and the page I'm sending the user to after they submit the form is my Devise registration page.

So I imagine I need something like this in the registrations controller action:

session[:text_entered] = params()

...but Devise doesn't give me a registrations controller. Do I need to make one?

Am I stuck with a super long URL when the form gets submitted? Should this be a POST or a GET? How do I pass the textarea value to the registrations page without sending it as a URL parameter?

Sorry I'm a newbie. Thanks for any help!

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

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

发布评论

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

评论(1

淡笑忘祈一世凡恋 2024-12-13 04:23:14

因此,您有一个文本区域所在页面的控制器,我们将其称为“SomethingsController”。据我所知,该页面上的表单提交到的控制器是 RegistrationsController。我不会在 RegistrationsController 中处理该表单的提交,而是让 SomethingsController 处理它。

当您将表单POST到SomethingsController(是的,您应该POST)时,它将触发create操作,然后您将从params<获取值/code> (这是一个哈希 - 您可以使用 [] 访问其值)并将其放入 session 中。完成后,您可以将用户重定向到注册页面。像这样的东西:

SomethingsController < ActionController::Base
  def create
    if text = params[:text_area_name] && text.present?
      session[:text_entered] = text
      redirect_to new_user_registration_path
    else
      flash[:error] = "You didn't enter any text!"
      render :action => :new
    end
  end
end

So, you have a controller for the page that the textarea is on, let's call it "SomethingsController." And the controller the form on that page submits to is, I gather, RegistrationsController. Instead of handling the submission of that form in RegistrationsController, what I would do is let SomethingsController handle it.

When you POST the form to SomethingsController (and yes, you should POST) it will fire the create action, and there you'll get the value from params (which is a Hash--you access its values with []) and put it in session. Once you've done that you can redirect the user to the registration page. Something like this:

SomethingsController < ActionController::Base
  def create
    if text = params[:text_area_name] && text.present?
      session[:text_entered] = text
      redirect_to new_user_registration_path
    else
      flash[:error] = "You didn't enter any text!"
      render :action => :new
    end
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文