2016-11-24 34 views
1

我已經使用此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

,易清潔的方法是創建一個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" %> 

和您的控制器索引操作(我不知道'welcome/index'是否在項目的控制器或聯繫人控制器上)

def index 
#your code 
@contact = Contact.new 
end 

最後,你似乎很新的軌道,我想推薦一個免費Ruby on Rails Tutorial

+0

非常感謝!完美的作品! :) –