Question

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?

Was it helpful?

Solution

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
    
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top