【问题标题】:Rails/devise app not working properly on Heroku due to new_user_registration_path由于 new_user_registration_path,Rails/devise 应用程序无法在 Heroku 上正常运行
【发布时间】:2016-09-02 14:16:00
【问题描述】:

希望你们中的一个可以帮助我解决这个问题,因为我已经为此烦恼了一段时间。

在 cloud9 预览版上试用后,我将我的应用程序部署到了 Heroku。 问题是,每当我尝试按下按钮为付费订阅创建帐户时,我都没有链接到该页面来填写我的凭据。但是,在 cloud9 上,这似乎工作正常。

通过用鼠标将鼠标悬停在不同平台上的相同按钮上,我发现了两者之间的唯一区别。

在 cloud9 上我得到:https://xxxxxxxx-xxxxx.xxxxxxx.xx/users/sign_up?plan=1

在 Heroku 上我得到:https://xxxxxxxx-xxxx-xxxxx.herokuapp.com/sign_up

所以每当我点击 Heroku 上的那个时,我都会收到内置的 Flash 消息:请选择会员计划进行注册!

更具体地说,虽然问题出在我看来,但我认为它在 home.html.erb 中的某个地方,变量通过该处。 (添加在下面)

所以看来我的 Heroku 上的路由工作不正确,但我就是不知道为什么。我在下面添加了我的文件。

希望有人可以帮助我解决这个问题! 亲切的问候。

宝石文件

source 'https://rubygems.org'


# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.5'
# Use sqlite3 as the database for Active Record
gem 'sqlite3', group: [:development, :test]
# Use postgresql as the database for production
group :production do
  gem 'pg'
  gem 'rails_12factor'
end
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.1.0'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby

# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0', group: :doc

# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

# Use Unicorn as the app server
# gem 'unicorn'

# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug'
end

group :development do
  # Access an IRB console on exception pages or by using <%= console %> in views
  gem 'web-console', '~> 2.0'

  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
end

### ADDED GEMS ###

# Bootstrap Gem
gem 'bootstrap-sass', '~> 3.3.6'

# Devise Gem
gem 'devise'

# Font Awesome
gem 'font-awesome-rails'

# Use stripe for handling payments
gem 'stripe'

# Use figaro to hide secret keys
gem 'figaro'

ruby '2.3.0'

routes.rb

Rails.application.routes.draw do
  devise_for :users, controllers: { registrations: 'users/registrations' }
  resources :users do
    resource :profile
  end
  resources :contacts
  get '/about' => 'pages#about'
  root 'pages#home'
end

pages_controller.rb

class PagesController < ApplicationController
  def home
    @trial_plan = Plan.find_by id: '1'
    @pro_plan = Plan.find_by id: '2'
  end

  def about
  end
end

users/registrations_controller.rb

class Users::RegistrationsController < Devise::RegistrationsController
  before_filter :select_plan, only: :new

  def create
    super do |resource|
      if params[:plan]
        resource.plan_id = params[:plan]
        if resource.plan_id == 2
          resource.save_with_payment
        else
          resource.save
        end
      end
    end
  end

  private
    def select_plan
      unless params[:plan] && (params[:plan] == '1' || params[:plan] == '2')
        flash[:notice] = "Please select a membership plan to sign up."
        redirect_to root_url
      end
    end
end

application.html.erb

<!DOCTYPE html>
<html>
<head>
  <title>Ayris</title>
  <%= stylesheet_link_tag    'application', media: 'all' %>
  <%= javascript_include_tag "https://js.stripe.com/v2/", type: 'text/javascript' %>
  <%= javascript_include_tag 'application' %>
  <%= tag :meta, :name => "stripe-key", :content => STRIPE_PUBLIC_KEY %>
  <%= csrf_meta_tags %>
</head>
<body>
...
      <%= link_to root_path, class: 'navbar-brand' do %>
        <i class="fa fa-users"></i>
        Ayris
      <% end %>
        ...
        <% if user_signed_in? %>
          ...
        <% else %>
          ...
        <% if user_signed_in? %>
          ...
        <% end %>
        <li><%= link_to "About", about_path %></li>
        <li><%= link_to "Contact Us", new_contact_path %></li>
      </ul>
    </div><!-- /.navbar-collapse -->
  </div>
</nav>

<div class="container">
  <% flash.each do |key, value| %>
    <%= content_tag :div, value, class: "alert alert-#{key}" %>
  <% end %>

  <%= yield %>
</div>

</body>
</html>

home.html.erb

  <% if user_signed_in? %>
    ...
        <% if current_user.profile %>
          ...
        <% else %>
          ...
        <% end %>
     ...
  <% else %>
    <div class="col-md-6">
      <div class="well">
        <h2 class="text-center">Trial Membership</h2>
        <h4>Sign up for free and get trial access for a period of 10 days.</h4>
        <br/>
        <%= link_to "Sign up for Trial", new_user_registration_path(plan: @trial_plan), class: 'btn btn-primary btn-lg btn-block' %>
      </div>
    </div>
    <div class="col-md-6">
      <div class="well">
        <h2 class="text-center">Pro Membership</h2>
        <h4>Sign up for the pro account for €75/month and get access to all functions.</h4>
        <%= link_to "Sign up for Pro", new_user_registration_path(plan: @pro_plan), class: 'btn btn-success btn-lg btn-block' %>
      </div>
    </div>
  <% end %>
</div>

【问题讨论】:

  • 您希望计划 ID 为 1 或 2。您应该通过登录控制台并运行 Plan.pluck :id 来验证这对两个主机都是正确的。我的直觉是其中一个有不同的 id。
  • 2.3.0 :001 &gt; Plan.pluck :id (0.4ms) SELECT "plans"."id" FROM "plans" =&gt; [1, 2] 这是控制台返回的内容
  • 因此,当计划 ID 为 1 或 2 时,您会设置该错误
  • 包含unlessbefore_filter 用于确定计划是否存在并且是1 或2 @diego.greyrobot
  • 您正在对 id 1 和 2 进行硬编码。不同数据库中的 ID 可能不同。取而代之的是他们的名字是相同的“试用计划”和“专业计划”,并通过名称而不是 id 查找计划

标签: ruby-on-rails ruby heroku devise


【解决方案1】:

我解决了这个问题,你提到的关于数据库的事情是正确的。问题是,由于某种原因,数据库迁移到 Heroku 是不成功的,没有给我任何通知。运行后:

Heroku run rails c
Plan.create(name: 'trial', price: 0)
Plan.create(name: 'pro', price: 75)

运行这些命令后,这两个计划都被注入到我的 Heroku 应用程序的数据库中。我的代码现在似乎工作得很好,尽管我已经根据您对硬编码 ID 的评论更改了以下代码

pages_controller.rb

class PagesController < ApplicationController
  def home
    if Plan.find_by name: 'trial'
      @trial_plan = 1
    end
    if Plan.find_by name: 'pro'
      @pro_plan = 2
    end
  end
end

感谢您的提醒。

【讨论】:

    猜你喜欢
    • 2012-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-21
    • 1970-01-01
    • 2016-04-07
    • 1970-01-01
    • 2015-11-30
    相关资源
    最近更新 更多