开发者

How do I save a textarea value to a Rails session?

开发者 https://www.devze.com 2023-04-08 15:38 出处:网络
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 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 s开发者_如何转开发ubmitted? 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!


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
0

精彩评论

暂无评论...
验证码 换一张
取 消