2016-03-01 32 views
1

我正在嘗試在頁面的頁腳中創建聯繫表單,因此它在我的應用程序視圖中。使用Ruby on Rails在頁腳中創建聯繫表單

我的應用程序控制器是這樣的:

class ApplicationController < ActionController::Base 
    # Prevent CSRF attacks by raising an exception. 
    # For APIs, you may want to use :null_session instead. 
    protect_from_forgery with: :exception 
    before_filter :configure_permitted_parameters, if: :devise_controller? 
    protected 

    @contact = Contact.new(contact_params) 
    if @contact.save 
    name = params[:contact][:name] 
    email = params[:contact][:email] 
    body = params[:contact][:comments] 
    ContactMailer.contact_email(name, email, body).deliver 
    flash[:success] = 'Message sent.' 
    redirect_to "" 
    else 
    flash[:success] = 'Error occured, message has not been sent.' 
    redirect_to "" 
    end 

    def configure_permitted_parameters 
    devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:name, :stripe_card_token, :email, :password, :password_confirmation) } 
    end 

    private 
    def contact_params 
     params.require(:contact).permit(:name, :email, :comments) 
    end 

end 

,我使用目前看起來像這樣在我的應用程序視圖中的部分觀點:

<%= form_for @contact do |f| %> 
    <div class="form-group"> 
     <%= f.label :name %> 
     <%= f.text_field :name, class: 'form-control' %> 
    </div> 
    <div class="form-group"> 
     <%= f.label :email %> 
     <%= f.email_field :email, class: 'form-control' %> 
    </div> 
    <div class="form-group"> 
     <%= f.label :comments %> 
     <%= f.text_area :comments, class: 'form-control' %> 
    </div> 
    <%= f.submit 'Submit', class: 'btn btn-default' %> 
<% end %> 

我敢肯定有一些錯誤但我無法理解控制器和模型如何在一起工作。

+0

請不要使用稱呼(「hi」),「謝謝」或簽名等虛假內容。 SO不是討論版,它就像一本參考書。 –

回答

0

您在protectedprivate之間的所有代碼都在該類的上下文中運行,因此在您的應用程序加載該文件時運行。它應該拋出一個異常undefined method or variable params for ApplicationController:Class

你需要爲每個請求的運行它,所以像:

class ApplicationController < ActionController::Base 
    # Prevent CSRF attacks by raising an exception. 
    # For APIs, you may want to use :null_session instead. 
    protect_from_forgery with: :exception 
    before_filter :configure_permitted_parameters, if: :devise_controller? 
    before_filter :set_contact 

    private 

    def set_contact 
    @contact = Contact.new 
    end 

    def configure_permitted_parameters 
    devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:name, :stripe_card_token, :email, :password, :password_confirmation) } 
    end 

end 

before_filter是它告訴你的控制器它應該在每次請求之前做的方法。你只需要設置@contact實例變量來顯示和結合的形式,形式,但應服從於ContactsController創建行動:

class ContactsController < ApplicationController 
    def create 
    @contact = Contact.new(contact_params) 
    if @contact.save 
     ContactMailer.contact_email(@contact.name, @contact.email, @contact.body).deliver 
     flash[:success] = 'Message sent.' 
     redirect_to :back 
    else 
     flash[:success] = 'Error occurred, message has not been sent.' 
     redirect_to :back 
    end 
    end 

    private 

    def contact_params 
    params.require(:contact).permit(:name, :email, :comments) 
    end 
end 

有一點要注意的是,before_filter將每一個動作之前運行,無論其類型(即使是重定向,當你不顯示任何東西時)。顯然,這是這就是爲什麼它可能是更好的做以下的浪費:

class ApplicationController < ActionController::Base 
    # Prevent CSRF attacks by raising an exception. 
    # For APIs, you may want to use :null_session instead. 
    protect_from_forgery with: :exception 
    before_filter :configure_permitted_parameters, if: :devise_controller? 
    helper_method :new_contact 

    private 

    def new_contact 
    Contact.new 
    end 

    def configure_permitted_parameters 
    devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:name, :stripe_card_token, :email, :password, :password_confirmation) } 
    end 
end 

#form 

<%= form_for new_contact do %> 

這樣新的接觸僅在需要時建立在內存中。

0

也許你應該爲聯繫人創建一個新的控制器:

# /app/controllers/contacts_controller.rb 
class ContactsController < ApplicationController 

    def new 
    @contact = Contact.new 
    end 

    def create 
    @contact = Contact.new(contact_params) 
    if @contact.save 
     ContactMailer.contact_email(@contact.name, @contact.email, @contact.comments).deliver 
     # Or you can send @contact object and find name, email etc. values in mailer 
     # ContactMailer.contact_email(@contact).deliver 
     flash[:success] = 'Message sent.' 
     redirect_to "" 
    else 
     flash[:success] = 'Error occured, message has not been sent.' 
     redirect_to "" 
    end 
    end 

    private 

    def contact_params 
    params.require(:contact).permit(:name, :email, :comments) 
    end 
end 

和你的形式將app/views/contacts/new.html.erb去。頁腳鏈接將如下所示:

<%= 'Contact Us', new_contact_path %>