RoR - 在静态页面上添加表单

RoR - Adding form over a static page

谁能告诉我如何在我的 rails 应用程序中添加一个简单的表单?我在单独的页面上创建了一个表单,它工作正常,但我如何在我的静态页面上实现它?

我的app/views/contact/_form.html.haml

.container
  %h1 Contact
  = simple_form_for @contact, :html => {:class => 'form-horizontal' } do |f|
    = f.input :name, :required => true
    = f.input :email, :required => true
    = f.input :message, :as => :text, :required => false, :input_html => {:rows => 10}

    .hidden
      = f.input :nickname, :hint => 'Leave this field blank!'
    .form-actions
      = f.button :submit, 'Send message', :class=> "btn btn-primary"

我的联系人控制器:

    class ContactsController < ApplicationController

    def new
        @contact = Contact.new
    end

    def create
        @contact = Contact.new(params[:contact])
        @contact.request = request
        if @contact.deliver
            flash.now[:error] = nil
        else
            flash.now[:error] = 'Cannot send message.'
            render :new
        end
    end

end

我要添加表格的地方,(index.html.haml)(静态页面)

#callouts2
.callout_inner
    .wrapper
        .callout
            =render 'contacts/form'

我的routes.rb

Rails.application.routes.draw do

  devise_for :users
  resources :posts do
    member do
      get "like", to: "posts#upvote"
      get "dislike", to: "posts#downvote"
    end
    resources :comments
  end

  authenticated :user do
    root 'posts#index', as: "authenticated_root"
  end

  resources "contacts", only: [:new, :create]

  root 'pages#index'

我在 views/contact/create 中还有另一个 'Thank you for your message' 页面。html.haml,我也需要将他们重定向到那个页面。

编辑:我在索引页中添加了一个 =render 'contacts/form',但它给了我一个错误:

ArgumentError in Pages#index Showing C:/Sites/blogger/app/views/contacts/_form.html.haml where line #3 raised:

First argument in form cannot contain nil or be empty Trace of template inclusion: app/views/pages/index.html.haml

如果您的表单名为“_form”,您可以这样做:

render "form"

我不熟悉 haml,不确定还需要什么其他语法,但这就是将表单添加到页面的方式。