创建一个简单的Rails

创建一个简单的Rails

本文介绍了创建一个简单的Rails 3文本帮手宝石的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究我的第一个Rails 3插件,以便在我的所有应用程序的 ApplicationHelper 中打包一个简单的帮助函数。您可以在Github上查看。



这是我第一次尝试:

  ## lib / semantic_id.rb ## 

require'semantic_id / version'

module :: ActionView :: Helpers :: TextHelper
$ b $ def semantic_id
string = String.new

case
当controller.action_name =〜/ new | edit /
string + = controller.action_name +_
when controller.action_name =〜/ index | create /
string + = controller.controller_name
else
string + = controller.controller_name.singularize
end

string + =_view
end

end

现在,这个方法可行,但据我所知,这不是'Rails 3 Way '扩展ActiveSupport或任何其他Rails模块。我一直无法找到关于如何构建构建Rails 3 Gem的文档。我尝试了下面的Rails指南,但是添加帮助器的方法不起作用,否则我错过了一些东西。



我的问题是:以上面的代码作为我正在寻找的功能的一个例子,你会如何将它变成一个Rails 3插件Gem?



谢谢! p>

解决方案

你所做的事情的问题是你已经将你的gem的功能绑定到ActionView。为了解决这个问题,你应该把你的代码打包到自己的模块中,然后将这个模块注入到你的视图环境中。例如,把你的助手放到它自己的模块(和文件)中:

$ $ p $ $ $ $ c $#lib / semantic_id / helper.rb

模块SemanticId
模块助手
def semantic_id
...
结束
结束
结束

然后使用Railtie将它添加到ActionView中:

  
require'semantic_id / helper'

ActiveSupport.on_load(:action_view)do
include SemanticId :: Helper
end

您希望将帮助器的功能部分与插入到其中的部分分开框架。你的方法包含一些特定于Rails的代码,但是如果它没有使用这种技术,你可以在Sinatra和其他框架中使用它。



通常,你项目的内容应该在 lib /< project_name> / *。rb lib /< project_name> .rb 应该让这个功能可用。


I've been working on my first Rails 3 plugin, to package up a simple helper function that I like having in the ApplicationHelper of all my apps. You can see the whole code on Github.

Here's my first attempt:

## lib/semantic_id.rb ##

require 'semantic_id/version'

module ::ActionView::Helpers::TextHelper

  def semantic_id
    string = String.new

    case
    when controller.action_name =~ /new|edit/
      string += controller.action_name + "_"
    when controller.action_name =~ /index|create/
      string += controller.controller_name
    else
      string += controller.controller_name.singularize
    end

    string += "_view"
  end

end

Now, this works, but as I understand it this is not the 'Rails 3 Way' of extending ActiveSupport or any other Rails module. I haven't been able to find much documentation on how you're "supposed" to build a Rails 3 gem. I tried following the Rails Guide, but the method given there for adding helpers didn't work, or else I was missing something.

My question is: given the code above as an example of the functionality I'm looking for, how would you turn this into a Rails 3 plugin Gem?

Thanks!

解决方案

The problem with what you've done is that you've bound your gem's functionality to ActionView. It would be better to make it more generic.

To fix this you should package your code in its own module and then inject that module into your view environment. For example, put your helper into its own module (and file):

# lib/semantic_id/helper.rb

module SemanticId
  module Helper
    def semantic_id
      ...
    end
  end
end

Then add it to ActionView using a Railtie:

# lib/semantic_id.rb

require 'semantic_id/helper'

ActiveSupport.on_load(:action_view) do
  include SemanticId::Helper
end

You want to separate the functional part of the helper from the part that inserts it into the framework. Your method contains some Rails-specific code, but if it didn't this technique would allow you to use it with Sinatra and other frameworks as well.

In general, the guts of your project should be in lib/<project_name>/*.rb, and lib/<project_name>.rb should just make the functionality accessible.

这篇关于创建一个简单的Rails 3文本帮手宝石的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 20:30