Question

I want people to be able to submit their email on the home page and then get redirected to the views/pages/about With the following code when they click submit on their email it takes them to http://localhost:3000/premails and gives the following error:

Routing Error
uninitialized constant PremailsController

I've tried adding code to my controller like this:

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

and other variations but they all give me a bunch of other problems and I haven't been able to figure out the cleanest simple way to do this

This is my pages_controller.rb

class PagesController < ApplicationController

    def home
        @premail = Premail.new
    end

    def about
    end


end

This is the form in my views/pages/home.html.erb

    <div class="container-form">
            <%= form_for @premail, html: {class: "form-inline", role: "form"} do |f| %>
                  <% if @premail.errors.any? %>                 
                      <h2><%= pluralize(@premail.errors.count, "error") %> prohibited this link from being saved:</h2>
                      <ul>
                      <% @premail.errors.full_messages.each do |msg| %>
                        <li><%= msg %></li>
                      <% end %>
                      </ul>
                  <% end %>
              <div class="form-group signup-field">
                <%= f.label :email, class:"sr-only" %>
                <%= f.email_field :email, class:"form-control signup-input", placeholder:"Enter email" %>
              </div>
              <div>
                <%= f.submit "Get Early Access", class:"btn btn-default signup-button" %>
              </div>
            <% end %>
    </div>

This is my routing:

  root 'pages#home'

  resources  :pages
  resources  :premails

This is my premail model

class Premail < ActiveRecord::Base

end

This is my migration:

class CreatePremails < ActiveRecord::Migration
  def change
    create_table :premails do |t|

        t.text :email

      t.timestamps
    end
  end
end

What would you change to re-route to the views/pages/about page and be able to retain the email(premail) in your database? Thanks!

Was it helpful?

Solution 2

The problem is in what your form_for @premails does when submitting it. Clicking the submit button on a form_for object will try to send the object either to the 'create' or 'update' action it's own controller depending on whether it's a new resource or not.

as the error message shows here you do not have a Premails controller. You either need to create a Premails controller with a create an update action or you need to change where the form sends the submit button to.

If you create the controller than in the create action you can redirect_to the about_page_path like this:

def create
   @premail = Premail.new(premail_params)
   if @premail.save
      redirect_to about_page_path
   else
      redirect_to :back
   end
end

Or if you dont want to create an entire premail controller you can change what the f.submit button does in your form to direct to an action in your pages controller. You would have to do something like

button_tag type: "submit"

The preferred method would be to create a premails controller.

OTHER TIPS

you need the premails_controller.rb to make the view interact with the premail model.

Now when you have the premails controller.

resources :premails

will work and form_for @premail will create a form for an individual Premail model object.

you will have to now make the @premail instance variable available here :

views/premails/new.html.erb

by using this :

class PremailsController < ApplicationController

    def new
        @premail = Premail.new
    end

    def create
        @premail = Premail.create(premail_params)
        if @premail.save
           redirect_to :action => :about
        end
    end

    def about
        render template: "premails/about"
    end

    private

    def premail_params
      params.require(:premail).permit(:email)
    end
end

make sure you have the about.html.erb page there inside the premails path

the problem is in what your form for premails does sumbitting it. Clicking the submit button on a form_for object will try to send the object either to the create or update action in your controller depending on whether it's a new resource or not.

as the error message shows here you do not have a premails controller. You either need to create a premails controller with a create an update action or you need to change where the form sends the submit button to. you can do this by changing that f.submit to a button_tag type submit.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top