有人能告诉我如何在我的 Rails 应用程序中添加一个简单的表单吗?我在单独的页面上创建了一个表单,它工作正常,但是如何在我的静态页面上实现它?
我的应用程序/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'
我的路线.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.html.haml 中还有另一个“感谢您的消息”页面,我也需要将它们重定向到该页面。
编辑:我=render 'contacts/form'
在我的索引页面中添加了一个,但它给了我一个错误:
Pages#index 中的 ArgumentError 显示 C:/Sites/blogger/app/views/contacts/_form.html.haml 其中第 3 行提出:
表单中的第一个参数不能包含 nil 或为空模板包含的痕迹:app/views/pages/index.html.haml