2

Try to build a welcome wizard and try to get existing rails code to be ported to be rails 4 compatible. Based mostly on previous great answer: https://stackoverflow.com/a/17255451/355281

I try to call http://books:3000/welcome/basics

This results in:

Circular dependency detected while autoloading constant WelcomeController

app/controllers/welcome_controller.rb

class Welcome::ApplicationController < ::ApplicationController
  layout "welcome" 
  before_filter :authentice_user!   
end

app/controllers/welcome_basics_controller.rb

class Welcome::BasicsControlller < Welcome::ApplicationController

  before_action :authenticate_user!
  before_filter :allowed?

  def new
    @step = Welcome::Basics.new(current_user)
  end

  def create
    @step = Welcome::Basics.new(current_user)
    if @step.save(params[:welcome_basics])
      redirect_to welcome_some_other_step_path, :notice => "Yay"
    else
      render :new
    end
  end

  private

  def step
    @step ||= Welcome::Basics.new(current_user)
  end

  helper_method :step

  def allowed?
    redirect_to previous_step_path unless step.allowed?
  end

end

**app/models/welcome_basics.rb

class Welcome::Basics

  include ActiveModel::Validations
  include ActiveModel::Conversion
  extend ActiveModel::Naming

  def persisted?
    false
  end

  def initialize(user)
    @user = user
  end

  attr_reader :user

  delegate :some_field, :some_other_field, :to => :user

  validates_presence_of :some_field

  def save(params)
    user.some_field = params[:some_field]
    user.some_other_field = params[:some_other_field]
    if valid?
      user.step = 2
      user.save
    end
  end

  def photo
    @photo ||= Photo.new
  end

  def profile
    @profile ||= user.profiles.first
  end

end

/config/routes.rb

  namespace :welcome do
    resource :basics, :only => [:new, :create]
  end
4

1 回答 1

4

我看到一些似乎不正常的事情。Rails 4 与 Rails 3.2 的最大问题似乎在于您的伪模型定义。

class Welcome::Basics

  include ActiveModel::Model # Necessary for Rails 4

  include ActiveModel::Validations
  include ActiveModel::Conversion
  extend ActiveModel::Naming

  ...

end

在你的welcome_controller.rb,你有authentice user哪个应该读authenticate user

在您welcome_basics_controller.rb的定义中,您拼错了控制器,目前:Welcome::BasicsControlller应该是Welcome::BasicsController

我还建议您将命名和继承更改为更简单,更符合 Rails 约定优于配置。这将是我的方法:

由于welcome_basics.rb 是一个伪模型,我将它放在它自己的目录中,以防止任何潜在的ActiveModel 自动采购。我还将名称更改为单数,以符合 Rails 模型的约定。

应用程序/pseudo_models/welcome_basic.rb

class WelcomeBasic

  include ActiveModel::Model # Necessary for Rails 4

  include ActiveModel::Validations
  include ActiveModel::Conversion
  extend ActiveModel::Naming

  ... 

end

现在我将你的welcome_controller 放在控制器目录中一个名为“welcome”的子目录中:

应用程序/控制器/欢迎/welcome_controller.rb

class Welcome::WelcomeController < ApplicationController
  layout "welcome" 
  before_filter :authenticate_user!   
end

那么你的 basics 控制器应该在“welcome”目录中被称为 basics,并且由于你继承自你的welcome 控制器,你不需要重复这authenticate_user!行。另请记住,您需要将对伪模型的调用从 重命名Welcome::BasicsWelcomeBasic。对于 Rails 4,您还需要在控制器中实现强参数。

应用程序/控制器/欢迎/basics_controller.rb

class Welcome::BasicsController < Welcome::WelcomeController

  before_filter :allowed?

  def new
    @step = WelcomeBasic.new(current_user)
  end

  def create
    @step = WelcomeBasic.new(current_user)
    if @step.save(basic_params) #updated to use strong_params
      redirect_to welcome_some_other_step_path, :notice => "Yay"
    else
      render :new
    end
  end

  private

  #For strong_params
  def basic_params
    params.require(:welcome_basic).permit(:attribute1, :attribute2)
  end


  def step
    @step ||= WelcomeBasic.new(current_user)
  end

  helper_method :step

  def allowed?
    redirect_to previous_step_path unless step.allowed?
  end

end

/config/routes.rb

...
  namespace :welcome do
    resources :basics, :only => [:new, :create]
  end
...

您需要确保的另一件事是您的相应视图与您的控制器放置在相同的目录结构中,因此您应该具有以下内容:

/app/views/welcome/basics/welcome.html.erb

另请注意,当您说“我尝试致电http://books:3000/welcome/basics”时,我假设您的意思是您尝试发布表格?否则,您应该打电话 http://books:3000/welcome/basics/new来获取您的基本表格。如果您希望该路线映射到http://books:3000/welcome/basics您需要在config/routes.rb文件中进行相应的调整。

于 2013-09-20T11:05:12.727 回答