1

我正在学习 Michael Hartl 的 Ruby on Rails 教程,并使用那里的模板为我的应用程序创建了一个联系人页面。但是,我想添加一个具有表单方法的联系人页面。我使用与 Hartl 相同的静态页面控制器和相同的页面。我需要让我的联系人页面正常工作。

<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 %>

我收到的错误消息涉及第一行不能为 nil 或类似的东西。作为记录,这是我的静态页面控制器。

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

运行 rake 路线显示了这一点

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

我想这就是我需要的所有信息。

4

2 回答 2

0

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

于 2014-06-12T07:06:38.330 回答
0

我相信static_pages_contact_path在你的例子中很可能是零。我相信您试图引用资源路由为您生成的路径之一。您可以尝试(绝对或相对)URL 或以一种或另一种方式使用资源路由。然而,控制器不是资源。

于 2014-06-12T01:39:34.630 回答