2015-02-09 44 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