好吧,我失败了。。。 没有成功的跑起来,虽然经过查看源代码等等修改,也还是一直有失败消息。我怀疑这个GEM 不兼容 3.2.11 ,或者作者已经停止维护了(上一次提交代码是2个月前,再一次是8个月前,还有20+ issue没有 解决, 好多pull request 没有处理) (OK, I admit that I failed to manage this to work. I don't think this gem is compatible with Rails 3.2.11, and its last commit is 2 month ago, last but 1 commit is 8 months ago, and a dozen issues not fixed, many pull requests not processed. )
这几天在调试 rubygem for rails (including views/controllers/... ) , 所以google到了apotomo, 记录一下。(these days I am creating a rails engine gem, so I found apotomo, a rails view components framework )
1. edit Gemfile: gem 'apotomo'
2. 假设已经存在一个 模块:post, 它"has_many comments", 那么就需要: run the generation command (assuming we already have a 'post' module which has_many 'comments' )
sg552@youku:/sg552/workspace/test-cell$ bundle exec rails generate apotomo:widget Comments display write -e haml
invoke haml
create app/widgets/comments/display.html.haml
create app/widgets/comments/write.html.haml
invoke test_unit
create test/widgets/comments_widget_test.rb
create app/widgets/comments_widget.rb
3. in post_controller:
class PostsController < ApplicationController
include Apotomo::Rails::ControllerMethods
has_widgets do |root|
root << widget(:comments, :post => @post)
end
def index
#...
end
end
4. in app/views/posts/show.html.haml
%h1 @post.title %p @post.body %p = render_widget :comments
5. 实现这个 widget (一个迷你型的controller )
class CommentsWidget < Apotomo::Widget
responds_to_event :post
def display(args)
@comments = args[:post].comments
render
end
def post(evt)
@comment = Comment.new(:book_id => evt[:book_id])
@comment.update_attributes evt[:comment] # a bit like params[].
update :state => :display
end
end
6. 最后, display.html.haml 中:
= widget_div do
%ul
- for c in @comments
%li c.text
- form_for :comment, @comment, :url => url_for_event(:post), :remote => true do |f|
= f.error_messages
= f.text_field :text
= submit_tag "Don't be shy, comment!"
好吧,我失败了。。。 没有成功的跑起来,虽然经过查看源代码等等修改,也还是一直有失败消息。我怀疑这个GEM 不兼容 3.2.11 ,或者作者已经停止维护了(上一次提交代码是2个月前,再一次是8个月前,还有20+ issue没有 解决, 好多pull request 没有处理)