Rails 插件生成器

Newbie note: you must run the generator in the Discourse source directory (that’s /src if you use Docker). Otherwise you’ll just get confusing error/help messages about how the parameters are incorrect for rails new.

5 个赞

There seems to be a mismatch between the plugin generator and this post. Specifically, the Beginner’s Guide suggests to create the javascript initializer with the following directory and naming structure:

plugins/basic-plugin/assets/javascripts/discourse/initializers/alert.js.es6

However, the plugin creates the javascript initializer at

plugins/discourse-racoon/assets/javascripts/initializers/discourse-racoon.es6

Personally, I was having issues with the javascript loading after using this plugin until I changed the directory and naming structure to match the Beginner’s guide. Should the package be updated?

4 个赞

Yes would be better indeed.

3 个赞

Yes I had to rename the JavaScript extensions but the structure worked for me.

this thing breaks for me.

Cant help you, you give me no context. What did you type? where did you type it? clean or exisisting state?

6 个赞

I followed this tutorial https://meta.discourse.org/t/beginners-guide-to-creating-discourse-plugins-part-1/30515. Just installed and worked so guess its clean.
I typed:

bundle exec rails g plugin cidian

EDIT: Capitalization matters in ruby. The name of the plugin created by this generator gets to be a ruby class so that’s why a lower case argument supplied to this generator will lead to a broken plugin.

No it’s not clean as you have followed this tutorial to create a plugin, and the error is likely in what you typed while following the tutorial.

The error is very clear to me: mount ::cidian::Engine, at: "/cidian" is invalid. You should fix this in your code.

2 个赞

First of all: I dont know ruby (yet) as I will start from the front end.
this code was produced by this plugin generator. so its not my fault. Its the generators fault. I typed nothing while following the tutorial. I just followed the development environment setup guide for ubuntu. Before running the generator the local discourse instance worked fine.
https://github.com/discourse/discourse/blob/master/lib/generators/plugin/templates/plugin.rb.erb
to be very clear: I am just at the first page of this tutorial. specifically the part that links to this plugin generator.

我刚刚对此进行了大规模更新。

更多内容即将推出:特定生成器(例如添加一个控制器)以及管理员插件。

12 个赞

哇!从差异对比来看效果很棒(对于一个对编写插件知之甚少的人来说)。迫不及待想试试了(不过我可能会等到周一到办公室再试:wink:)

2 个赞

嘿,@j.jaffeux,我刚刚试了一下(第一次失败后,我又执行了 git pull 并基于此创建了一些新插件),但我遇到了以下错误:

/home/pfaffman/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/actionpack-5.2.3/lib/action_dispatch/routing/mapper.rb:314:in `block (2 levels) in check_controller_and_action': 'one_two/one-two' is not a supported controller name. This can lead to potential routing problems. See http://guides.rubyonrails.org/routing.html#specifying-a-controller-to-use (ArgumentError)

我感到很沮丧,因为我有一个基于此启动的插件,在运行之前我已经往里面添加了很多代码。你能告诉我如何修复这个问题吗?那个指南里的内容以及我在 StackOverflow 上找到的信息都帮不上忙。

谢谢。

哦,是的,我偶然发现了这个,但还没修复。

我觉得应该是 controllers/one_two/one_two_controller.rb,不过因为你没有提供所有数据,我也不太确定。

4 个赞

看起来我找到了!问题出在 config/routes.rb 中。我将

 get "/" => "three-four#index", constraints: ThreeFourConstraint.new

改为

get “/” => “three_four#index”, constraints: ThreeFourConstraint.new

现在一切正常,至少从我这个外行的角度来看是这样。

我该如何着手处理这个问题?

尝试在我的新 Debian 环境(Windows 子系统)中使用此功能。
我的 Discourse 可以正常工作并运行,但当我尝试运行

rails g plugin --help

时,出现以下错误:

/var/lib/gems/2.5.0/gems/railties-6.0.0/lib/rails/app_loader.rb:53: 警告:PATH 中存在不安全的世界可写目录 /home/jose/.rbenv/shims,权限模式为 040777
无法在任何源中找到 actionview_precompiler-0.2.1
请运行 `bundle install` 以安装缺失的 gem。

运行 bundle install 没有任何效果(新的),而且 precompiler 已出现在 gem 列表中。
有什么建议吗?

您的 Discourse 服务器是否在 Docker 容器中运行?如果是,您需要在容器内部运行 bundle install

不,我不这么认为。这是一个开发实例,它是通过直接 git clone 获取的。

这开箱即用,效果完美。

非常感谢。

刚刚运行了这个 Rails 生成器,为新插件创建了基础结构,上传到 Git,添加到 yml 文件中,重新构建应用后,它完美地开箱即用。

访问插件标签页,它赫然在列,并配备了开箱即用的启用/禁用设置。:heart:

非常感谢 @j.jaffeux 提供这个简单易用且实用的生成器。

我唯一做的修改是删除了内置的 .git 目录(使用 rm -rf),然后重新初始化 Git,以便将其推送到我的账户;但这很可能是因为我不太擅长 git,经常需要像用锤子敲 git 命令 一样折腾才能让它按我期望的方式工作。

完美的生成器。再次感谢 @j.jaffeux

4 个赞

很高兴它对你有效。老实说,我应该花些时间在上面,因为还有很多地方运行不正常。

6 个赞