本文介绍了Rails 4欢迎向导,如何更正此代码以使其适用于rails 4?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试构建一个受欢迎的向导,并尝试将现有导轨代码移植到兼容rails 4的环境中。主要基于以前的很好的答案:

  
$ b

在 welcome_basics_controller.rb 您的定义中拼写错误的控制器,当前:欢迎: :BasicsControlller 应该是 Welcome :: BasicsController 。



我也建议您将命名和继承更改为简单一点,并且更符合Rails约定的配置。这是我的做法:

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


$ b

app / pseudo_models / welcome_basic.rb

  class WelcomeBasic 

包含ActiveModel :: Model#Rails需要4

包括ActiveModel ::验证
包含ActiveModel ::转换
扩展ActiveModel ::命名

...

结束

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



app / controllers / welcome / welcome_controller.rb

  class欢迎:: WelcomeController< ApplicationController 
layoutwelcome
before_filter:authenticate_user!
end

然后,您的基本控制器应该被称为welcome目录中的基础知识,因为您从您的欢迎控制器继承,您不需要重复 authenticate_user!行。另请注意,您需要将您的调用重命名为 Welcome :: Basics 至 WelcomeBasic 的伪模型。对于Rails 4,您还需要在控制器中实现强大的参数。


$ b

app / controllers / welcome / basics_controller.rb

  class Welcome :: BasicsController< Welcome :: WelcomeController 

before_filter:允许吗?
$ b $新增
@step = WelcomeBasic.new(current_user)
结束

def创建
@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
渲染:新的
结束
结束

私人

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


def步骤
@step || = WelcomeBasic .new(current_user)
结束

helper_method:step

def允许?
redirect_to previous_step_path,除非step.allowed?
结束

结束

/ config / routes .bb

  ... 
名称空间:welcome do
资源:基本信息,:只有=> [: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 文件。


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
解决方案

I see a few things that appear to be off. The one that appears to be the biggest issue with Rails 4 vs Rails 3.2 is in your pseudo-model definition.

class Welcome::Basics

  include ActiveModel::Model # Necessary for Rails 4

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

  ...

end

In your welcome_controller.rb, you have authentice user which should read authenticate user.

In your welcome_basics_controller.rb you misspelled controller in your definition, currently: Welcome::BasicsControlller should be Welcome::BasicsController.

I'd also recommend that you change your naming and inheritance to be a bit simpler and more inline with Rails convention over configuration. This would be my approach:

Since welcome_basics.rb is a pseudo-model, I'd put it in its own directory to prevent any potential automatic sourcing of ActiveModel. I'd also change the name to the singular to be in line with Rails conventions for models.

app/pseudo_models/welcome_basic.rb

class WelcomeBasic

  include ActiveModel::Model # Necessary for Rails 4

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

  ... 

end

Now I'd put your welcome_controller in a sub-directory called "welcome" within the controller directory:

app/controllers/welcome/welcome_controller.rb

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

Then your basics controller should be called basics within the "welcome" directory, and since your inheriting from your welcome controller, you don't need to repeat the authenticate_user! line. Also remember that you'll need to rename your calls to your pseudo-model from Welcome::Basics to WelcomeBasic. For Rails 4 you'll also need to implement strong params in your controller.

app/controllers/welcome/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
...

The other thing that you'll need to ensure is that your corresponding views are placed within the same directory structure as your controller, so you should have the following:

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

One other note, when you say "I try to call http://books:3000/welcome/basics", I assume you mean that your attempting to post your form? Otherwise, you should be calling http://books:3000/welcome/basics/new in order to get your basics form. If you want that route to map to http://books:3000/welcome/basics you'll need to make the corresponding adjustments in your config/routes.rb file.

这篇关于Rails 4欢迎向导,如何更正此代码以使其适用于rails 4?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 14:50