How do I define a custom URL for a form confirmation page?

情到浓时终转凉″ 提交于 2019-12-12 02:54:07

问题


I am creating a basic product landing page with Rails in which users can enter their email address to be notified when the product launches. (Yes, there are services/gems etc that could do this for me, but I am new to programming and want to build it myself to learn rails.)

On successful submit of the form, I would like to redirect to a custom '/thanks' page in which I thank users for their interest in the product (and also encourage them to complete a short survey.)

Currently, successful submits are displayed at "/invites/:id/" eg "invites/3" which I do not want since it exposes the number of invites that have been submitted. I would like to instead redirect all successful submits to a "/thanks" page.

I have attempted to research "rails custom URLs" but have not been able to find anything that works. The closest I was able to find was this Stackoverflow post on how to redirect with custom routes but did not fully understand the solution being recommended. I have also tried reading the Rails Guide on Routes but am new to this and did not see anything that I understood to allow for creating a custom URL.

I have placed my thanks message which I would like displayed on successful form submit in "views/invites/show.html.haml"

My Routes file

resources :invites
root :to => 'invites#new'

I tried inserting in routes.rb:

post "/:thanks" => "invites#show", :as => :thanks

But I don't know if this would work or how I would tell the controller to redirect to :thanks

My controller (basically vanilla rails, only relevant actions included here):

def show
    @invite = Invite.find(params[:id])
    show_path = "/thanks"

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @invite }
    end
  end

# GET /invites/new
# GET /invites/new.json
def new
  @invite = Invite.new

  respond_to do |format|
    format.html # new.html.erb
    format.json { render json: @invite }
  end
 end

 # POST /invites
 # POST /invites.json
 def create
   @invite = Invite.new(params[:invite])

   respond_to do |format|
     if @invite.save
       format.html { redirect_to @invite }
       #format.js { render :action => 'create_success' }
       format.json { render json: @invite, status: :created, location: @invite }
     else
       format.html { render action: "new" }
       #format.js { render :action => 'create_fail' }
       format.json { render json: @invite.errors, status: :unprocessable_entity }
     end
   end
 end

It would seem as if creating a standard URL for displaying a confirmation would be relatively straightforward. Any advice on how to achieve this would be appreciated.


回答1:


I guess you want to redirect after your create action, which is executed when the form is submitted.

Just add redirect_to in the following way:

def create
  @invite = Invite.new(params[:invite])

  if @invite.save
    ...
    redirect_to '/thanks'
  else
    ...
    redirect_to new_invite_path # if you want to return to the form submission page on error
  end
end

I omitted some of the code for brevity.

In your routes add:

get '/thanks', to: "invites#thanks"

Add the thanks action to your invites controller:

def thanks
  # something here if needed
end

And create a thanks.html.erb page in app/views/invites.




回答2:


I would do get "/thanks" => "invites#thanks" in routes.rb and then add this in your controller:

def thanks
end

Then add a file app/views/invites/thanks.html.erb with your thank-you content.




回答3:


You could create a route like this:

resources :invites do
  collection do
    get 'thanks'
  end
end

This will also create a path helper called thanks_invites_path.

It will be at the invites/thanks path, but if you want it to be on/thanks, you could just do as Jason mentioned:

get "/thanks" => "invites#thanks", :as => :thanks

The as part will generate a helper to access that page: thanks_path.

You would need a extra action in the controller called thanks, and put whatever info you need inside, and also you will need a additional view called thanks.html.erb

Since you want everybody to go to that page after a successful submit, in your create action you would have:

format.html { redirect_to thanks_invites_path} (or thanks_path), what ever you choose, when you name the route you can check it with rake routes if it's okay, and whatever rake routes says, just add _path at the end.



来源:https://stackoverflow.com/questions/15935072/how-do-i-define-a-custom-url-for-a-form-confirmation-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!