2015-02-09 46 views
0

我覺得我缺少一些簡單的東西。我建立了一個MailChimp郵件列表,我試圖讓註冊按鈕正常工作,但得到提交錯誤。我已經將它路由回控制器中create方法的根目錄,但它不起作用。沒有路由匹配[郵政]「/」 - 試圖設置郵件列表

signup.rb

class Signup < ActiveRecord::Base 
    validates_presence_of :email 
    validates_format_of :email, :with => /\A[-a-z0-9_+\.]+\@([-a-z0-9]+\.)+[a-z0-9]{2,4}\z/i 

    def subscribe 
     mailchimp = Gibbon::API.new 
     result = mailchimp.lists.subscribe({ 
     :id => ENV['MAILCHIMP_LIST_ID'], 
     :email => {:email => self.email}, 
     :double_optin => false, 
     :update_existing => true, 
     :send_welcome => true 
     }) 
     Rails.logger.info("Subscribed #{self.email} to MailChimp") if result 
    end 

end 

signups_controller.rb

class SignupsController < ApplicationController 

    def new 
     @signup = Signup.new 
    end 

    def create 
     @signup = Signup.new(secure_params) 
     if @signup.valid? 
      redirect_to root_path 
     else 
      render :new 
     end 
    end 

    private 

    def secure_params 
     params.require(:signup).permit(:email) 
    end 

end 

的routes.rb

Rails.application.routes.draw do 

    root 'pages#index' 
    get '/about' => 'pages#about' 
    get '/tour' => 'pages#tour' 
    get '/music' => 'pages#music' 

    resources :signups, only: [:new, :create] 
end 

我可以把routes.rb中怎麼得到這個文章?這裏是我的耙路線輸出...

Prefix Verb URI Pattern   Controller#Action 
     root GET/     pages#index 
    about GET /about(.:format)  pages#about 
     tour GET /tour(.:format)  pages#tour 
    music GET /music(.:format)  pages#music 
    signups POST /signups(.:format)  signups#create 
new_signup GET /signups/new(.:format) signups#new 

在此先感謝!

+0

你究竟如何得到這個錯誤?你能向我們展示視圖形式嗎? – dgilperez 2015-02-09 03:23:31

+0

另外,你既沒有保存註冊,也沒有調用''#subscribe'',順便說一句。 – dgilperez 2015-02-09 03:24:29

+0


<%= simple_form_for:signup do | f | %> <%= f.input:電子郵件,標籤:假,:佔位符=> '[email protected]' %>
\t \t \t <%= f.submit '註冊',:類=> 'BTN BTN-危險' %> \t \t \t <% end %> \t \t \t
MCHLWRRN 2015-02-09 03:45:53

回答

0

變化

<%= simple_form_for :signup do |f| %> 

<%= simple_form_for @signup do |f| %> 

,你就會路線SignupsController#create

除此之外,您需要在代碼中的任意位置撥打Signup#subscribe才能使整個表單正常工作。也許你想把註冊保存到數據庫中。我建議在控制這種變化:

def create 
    @signup = Signup.new(secure_params) 
    if @signup.save && @signup.subscribe 
     redirect_to root_path 
    else 
     flash[:error] = 'Ooops!' 
     render :new 
    end 
end 

並記得從Signup#subscribe返回true如果result成功。

+0

導致這種誤差從之前: http://stackoverflow.com/questions/28363650/sign-up-form-error-undefined - 方法模型名稱換nilclassclass – MCHLWRRN 2015-02-09 04:10:47