Ruby on rails RubyonRails-静态页面联系人表单

Ruby on rails RubyonRails-静态页面联系人表单,ruby-on-rails,railstutorial.org,contact-form,form-for,static-pages,Ruby On Rails,Railstutorial.org,Contact Form,Form For,Static Pages,我遵循了MichaelHartl的RubyonRails教程,并使用其中的模板为我的应用程序创建了一个联系人页面。但是,我想添加一个具有表单方法的联系人页面。我使用与Hartl相同的静态页面控制器来处理相同的页面。我需要让我的联系人页面正常工作 <h1>Contact us</h1> <%= form_for @page, url: static_pages_contact_path do |f| %> <p> <%= f.l

我遵循了MichaelHartl的RubyonRails教程,并使用其中的模板为我的应用程序创建了一个联系人页面。但是,我想添加一个具有表单方法的联系人页面。我使用与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 %>

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

我相信在你的例子中,静态页面联系路径很可能是零。我相信您是在尝试引用通过资源路由为您生成的路径中的一条。您可以尝试使用绝对或相对URL,或者以某种方式使用资源丰富的路由。但是,控制器不是资源。

您遇到的问题如下:

-

职位

您的路由只有定义为GET请求的静态\u页面\u联系人

您的表单将希望提交一个POST请求,而您没有该请求的路由,因此您将收到一个错误。解决这个问题的方法有两个方面-

在路由中创建POST请求 在控制器中创建相应的操作 修复当前控制器的问题
这将为您解决问题

您最好将具体错误发布给我们查看!
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
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
#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