【问题标题】:No route matches [POST] "/" - trying to set up mailing list没有路线匹配 [POST] "/" - 尝试设置邮件列表
【发布时间】:2015-02-09 03:09:44
【问题描述】:

我觉得我在这里遗漏了一些简单的东西。我设置了一个 MailChimp 邮件列表,我试图让注册按钮工作,但提交时出现错误。我已在控制器的 create 方法中将其路由回根目录,但它不起作用。

注册.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 中添加什么来发布此内容?这是我的 rake 路由输出...

    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

提前致谢!

【问题讨论】:

  • 你到底是怎么得到这个错误的?你能给我们看一下视图吗?
  • 另外,你既没有保存注册也没有调用#subscribe,顺便说一句。

  • 'cantwaittoseeyou@ournextshow.com' %>
    'btn btn -danger' %>

标签: ruby-on-rails routes mailchimp


【解决方案1】:

改变

<%= 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

如果result 成功,请记住从Signup#subscribe 返回true

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多