-1

I have a preview page up with a form that takes in emails(@premails). I've created a model & migration for this.

I have a pages controller with a Home, About & Contact actions and corresponding views.

After they submit their email on the Home page, I want to redirect them to a static About page. I have not been able to achieve this.

this is my pages controller:

class PagesController < ApplicationController

    def home
        @premail = Premail.new
        if @premail.save
            redirect_to about_path
        else
            render home_path
        end
    end

    def about
    end


end

But when I open my localhost with this code I get:

NameError in PagesController#home
undefined local variable or method `about_path' for #<PagesController:0x337ac40>

How can I make this happen?

tereško
  • 58,060
  • 25
  • 98
  • 150
user3408293
  • 1,377
  • 6
  • 18
  • 26

1 Answers1

0

For your case, use:

    if @premail.save
        redirect_to :action => :about
    end

else is not needed here, since by default Rails would render app/views/pages/home.html.erb, be sure you have this file.

Also when you redirect to about, you will need app/views/pages/about.html.erb file to be present.

Update

Seems you don't have this route in config/routes.rb, for Rails 3.x:

match ':controller(/:action(/:id))'

In Rails 4:

match ':controller(/:action(/:id))', :via => [:get , :post]

If you are planning to just answer to get, i.e. there are nor forms posting to controllers:

get ':controller(/:action(/:id))'

This will detect routes like localhost:3000/asd/qwe/1 and:

  1. Use asd as controller AsdController
  2. Use qwe as action:

    class AsdController
      def qwe; end
    
  3. params[:id] would be equal to 1.

  4. () means optional, for example if you go in your browser to localhost:3000/asd, Rails would call Asd#index, i.e.:

    class AsdController
      def index
        # whatever you have here
      end
    
sites
  • 21,417
  • 17
  • 87
  • 146
  • I have this 'resources :pages' does this not work? Is there a better way to do this without having to do this, I've heard that using 'resources' syntax is better.. What format could I pass to 'redirect_to' without modifying other code to redirect to the about page? – user3408293 Apr 30 '14 at 03:29
  • I have done all this this and get the following error: No route matches {:action=>"about", :controller=>"pages"} – user3408293 Apr 30 '14 at 03:42
  • Why is this needed? I've never seen this... What does it do? – user3408293 Apr 30 '14 at 03:49
  • BTW this gives me another error: You should not use the `match` method in your router without specifying an HTTP method. If you want to expose your action to both GET and POST, add `via: [:get, :post]` option. If you want to expose your action to GET, use `get` in the router: Instead of: match "controller#action" Do: get "controller#action – user3408293 Apr 30 '14 at 03:50
  • ah right, in Rails 4 generic `match` calls are not allowed, you can change match with get. but if you have a form you could do: `match ':controller...', :via => [:get, :post]` – sites Apr 30 '14 at 03:55
  • Where can I learn more about this? The documentation does a poor job of explaining this I haven't been able to understand why this is needed and how to properly format routing issues – user3408293 Apr 30 '14 at 03:59
  • there is a link in my answer. – sites Apr 30 '14 at 04:09