有人可以告诉我如何在我的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.html.haml中有另一个“感谢您的消息”页面,我还需要将它们重定向到该页面。
编辑:我在索引页面中添加了=render 'contacts/form'
,但它给了我一个错误:
Pages#index中的ArgumentError显示 C:/Sites/blogger/app/views/contacts/_form.html.haml第3行引发:
表单中的第一个参数不能包含nil或者是空的Trace 模板包含:app / views / pages / index.html.haml
答案 0 :(得分:1)
如果您的表单被调用&#34; _form&#34;,您可以执行以下操作:
# php -i | find "Architecture"
# php -i | find "Thread"
我不熟悉haml,不确定需要哪些其他语法,但这就是将表单添加到页面中的方式。