我已使用 example 在Rails 4中创建了一个联系表单 但我想在我的应用程序的主/显示页面上显示此联系表单。我怎么能这样做?
的routes.rb
Rails.application.routes.draw do
resources :projects
resources :contacts, only: [:new, :create]
get 'welcome/index'
root 'welcome#index'
end
contacts_controller.rb
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
感谢。
答案 0 :(得分:0)
简单而干净的方法是创建partial
_contact_form.html.erb(部分始终以下划线开头)
.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"
然后,在索引页面中:
<%= render "contacts/contact_form" %>
并在您的控制器索引操作中(我不知道欢迎/索引&#39;是否在项目的控制器或联系人控制器上)
def index
#your code
@contact = Contact.new
end
最后,你似乎对rails非常陌生,我想推荐一个免费的Ruby on Rails Tutorial