user3713082
user3713082

Reputation:

Ruby on Rails - Static Pages Contact Form

I was following the Ruby on Rails tutorial by Michael Hartl, and I used the template there to create a contacts page for my application. However, I would like to add a contacts page that has a form method. I use the same Static Pages Controller as Hartl with the same pages. I need to get my contacts page to work.

<h1>Contact us</h1>

<%= form_for @page, url: static_pages_contact_path do |f| %>

  <p>
    <%= f.label :title %>
    <%= f.text_field :title %>
  </p>

  <p>
    <%= f.label :email %>
    <%= f.text_area :email %>
  </p>

  <%= f.submit 'Send message' %>

<% end %>

The error message I get involves first row can't be nil or something like that. For the record, here is my static-pages-controller.

class StaticPagesController < ApplicationController

  def show
    @page = StaticPage.find(params[:id])
  end

  def new
    @page = Page.new
  end

  def create
    @page = Page.find(params[:page])
  end
end

Running rake routes shows this

Prefix Verb   URI Pattern                     Controller#Action
       users_new GET    /users/new(.:format)            users#new
   favorite_game PUT    /games/:id/favorite(.:format)   games#favorite
           games GET    /games(.:format)                games#index
                 POST   /games(.:format)                games#create
        new_game GET    /games/new(.:format)            games#new
       edit_game GET    /games/:id/edit(.:format)       games#edit
            game GET    /games/:id(.:format)            games#show
                 PATCH  /games/:id(.:format)            games#update
                 PUT    /games/:id(.:format)            games#update
                 DELETE /games/:id(.:format)            games#destroy
           users GET    /users(.:format)                users#index
                 POST   /users(.:format)                users#create
        new_user GET    /users/new(.:format)            users#new
       edit_user GET    /users/:id/edit(.:format)       users#edit
            user GET    /users/:id(.:format)            users#show
                 PATCH  /users/:id(.:format)            users#update
                 PUT    /users/:id(.:format)            users#update
                 DELETE /users/:id(.:format)            users#destroy
        sessions POST   /sessions(.:format)             sessions#create
     new_session GET    /sessions/new(.:format)         sessions#new
         session DELETE /sessions/:id(.:format)         sessions#destroy
 users_favorites GET    /users/favorites(.:format)      users#favorites
static_pages_about GET    /static_pages/about(.:format)   static_pages#about
static_pages_contact GET    /static_pages/contact(.:format) static_pages#contact
static_pages_help GET    /static_pages/help(.:format)    static_pages#help
          signup GET    /signup(.:format)               users#new
          signin GET    /signin(.:format)               sessions#new
         signout DELETE /signout(.:format)              sessions#destroy

I think that's all the information I need.

Upvotes: 1

Views: 625

Answers (2)

Richard Peck
Richard Peck

Reputation: 76774

The problem you've got is the following:

--

POST

Your routes have only got static_pages_contact defined as a GET request

Your form will want to submit a POST request, which you don't have a route for, hence the error you're receiving. The way to fix this is two-fold -

  1. Create a POST request in your routes
  2. Create a corresponding action in your controller
  3. Fix current controller problems
#config/routes.rb
resources :static_pages, only: [] do
    collection do
        get :about, action: "show"
        get :help, action: "show"
        get :contact, action: "show"
        post :contact_submit
    end
end

#app/controllers/static_pages_controller.rb
class StaticPagesController < ApplicationController
   def contact_submit
     ... perform form submit here
   end
end

This should fix your problem for you

Upvotes: 0

Michiel Boekhoff
Michiel Boekhoff

Reputation: 231

I believe that static_pages_contact_path in your example could very well be nil. I believe you were trying to refer to on of the paths generated for you by resourceful routing. You could try an (absolute or relative) URL or use resourceful routing in one way or another. Controllers are, however, not a resource.

Upvotes: 0

Related Questions