Blogged by Ujihisa. Standard methods of programming and thoughts including Clojure, Vim, LLVM, Haskell, Ruby and Mathematics written by a Japanese programmer. github/ujihisa

Monday, May 25, 2009

Trying Merb

I tried My First Blog Tutorial which is documented in Merb official site.

Install

Via http://wiki.merbivore.com/installing_merb_from_edge

I'm not interested in any stable versions of software, so I decided to install edge merb.

$ git clone git://github.com/wycats/merb.git
$ cd merb
$ sudo rake install

I got messages include errors.

WARNING:  description and summary are identical
mv merb-1.1.gem pkg/merb-1.1.gem
Failed to install gem '/Users/ujihisa/git/merb/merb/pkg/merb-1.1.gem (1.1)' (merb requires merb_datamapper (= 1.1, runtime))
WARNING:  no rubyforge_project specified
WARNING:  description and summary are identical
mv merb-core-1.1.gem pkg/merb-core-1.1.gem
Successfully installed merb-core-1.1
Writing executable wrapper /opt/local/bin/merb
WARNING:  description and summary are identical
mv merb_datamapper-1.1.gem pkg/merb_datamapper-1.1.gem
Failed to install gem '/Users/ujihisa/git/merb/merb_datamapper/pkg/merb_datamapper-1.1.gem (1.1)' (merb_datamapper requires merb-actionorm (~> 1.1, runtime))
WARNING:  description and summary are identical
mv merb-action-args-1.1.gem pkg/merb-action-args-1.1.gem
Successfully installed merb-action-args-1.1
WARNING:  description and summary are identical
mv merb-assets-1.1.gem pkg/merb-assets-1.1.gem
Successfully installed merb-assets-1.1
WARNING:  description and summary are identical
mv merb-slices-1.1.gem pkg/merb-slices-1.1.gem
Successfully installed merb-slices-1.1
Writing executable wrapper /opt/local/bin/slice
WARNING:  description and summary are identical
mv merb-actionorm-1.1.gem pkg/merb-actionorm-1.1.gem
Successfully installed merb-actionorm-1.1
WARNING:  description and summary are identical
mv merb-auth-core-1.1.gem pkg/merb-auth-core-1.1.gem
WARNING:  description and summary are identical
mv merb-auth-more-1.1.gem pkg/merb-auth-more-1.1.gem
WARNING:  description and summary are identical
mv merb-auth-slice-password-1.1.gem pkg/merb-auth-slice-password-1.1.gem
WARNING:  description and summary are identical
mv merb-auth-1.1.gem pkg/merb-auth-1.1.gem
Successfully installed merb-auth-core-1.1
Successfully installed merb-auth-more-1.1
Successfully installed merb-auth-slice-password-1.1
Successfully installed merb-auth-1.1
WARNING:  description and summary are identical
mv merb-cache-1.1.gem pkg/merb-cache-1.1.gem
Successfully installed merb-cache-1.1
WARNING:  no rubyforge_project specified
WARNING:  description and summary are identical
mv merb-exceptions-1.1.gem pkg/merb-exceptions-1.1.gem
Successfully installed merb-exceptions-1.1
WARNING:  description and summary are identical
mv merb-gen-1.1.gem pkg/merb-gen-1.1.gem
Successfully installed merb-gen-1.1
Writing executable wrapper /opt/local/bin/merb-gen
WARNING:  description and summary are identical
mv merb-haml-1.1.gem pkg/merb-haml-1.1.gem
Successfully installed merb-haml-1.1
WARNING:  description and summary are identical
mv merb-helpers-1.1.gem pkg/merb-helpers-1.1.gem
Successfully installed merb-helpers-1.1
WARNING:  description and summary are identical
mv merb-mailer-1.1.gem pkg/merb-mailer-1.1.gem
Successfully installed merb-mailer-1.1
WARNING:  description and summary are identical
mv merb-param-protection-1.1.gem pkg/merb-param-protection-1.1.gem
Successfully installed merb-param-protection-1.1
ERROR:  could not find gem pkg/merb-more-1.1.gem locally or in a repository

I tried to go to next step without considering those errors.

$ cd merb/merb-core
$ sudo rake install
$ cd ../merb-auth
$ sudo rake install

They succeeded.

My First Blog

$ cd ~/tmp
$ merb-gen app my_first_merblog --template-engine=haml
$ cd my_first_blog
$ ls
Rakefile  app/      autotest/ config/   doc/      gems/     merb/ public/   spec/     tasks/

cool.

$ merb-gen resource post title:string,body:string
Loading init file from /Users/ujihisa/tmp/my_first_merblog/config/init.rb
Loading /Users/ujihisa/tmp/my_first_merblog/config/environments/development.rb
 ~
 ~ FATAL: The gem merb_datamapper (>= 0, runtime), [] was not found
 ~

Noooo! I don't know whether the last errors caused or not, but I tried below

$ cd ~/git/merb/merb_datamapper/
$ sudo rake install
$ cd -
$ merb-gen resource post title:string,body:string
Loading init file from /Users/ujihisa/tmp/my_first_merblog/config/init.rb
Loading /Users/ujihisa/tmp/my_first_merblog/config/environments/development.rb
Generating with resource generator:
Loading /Users/ujihisa/tmp/my_first_merblog/config/environments/development.rb
Loading /Users/ujihisa/tmp/my_first_merblog/config/environments/development.rb
Loading /Users/ujihisa/tmp/my_first_merblog/config/environments/development.rb
     [ADDED]  spec/models/post_spec.rb
     [ADDED]  app/models/post.rb
     [ADDED]  spec/requests/posts_spec.rb
     [ADDED]  app/views/posts/show.html.haml
     [ADDED]  app/views/posts/index.html.haml
     [ADDED]  app/views/posts/edit.html.haml
     [ADDED]  app/views/posts/new.html.haml
     [ADDED]  app/controllers/posts.rb
     [ADDED]  app/helpers/posts_helper.rb
resources :posts route added to config/router.rb

Yay!

Contrary to rails, specs has been generated instead of tests. Let's spec!

$ rake spec
(in /Users/ujihisa/tmp/my_first_merblog)
Loading init file from /Users/ujihisa/tmp/my_first_merblog/config/init.rb
Loading /Users/ujihisa/tmp/my_first_merblog/config/environments/development.rb
./spec/models/../spec_helper.rb:17: uninitialized constant Merb::Test::ViewHelper (NameError)
        from /opt/local/lib/ruby/gems/1.8/gems/rspec-1.2.6/lib/spec/runner.rb:41:in `configure'
        from ./spec/models/../spec_helper.rb:16
        from ./spec/models/post_spec.rb:1:in `require'
        from ./spec/models/post_spec.rb:1
        from /opt/local/lib/ruby/gems/1.8/gems/rspec-1.2.6/lib/spec/runner /example_group_runner.rb:15:in `load' 
        from /opt/local/lib/ruby/gems/1.8/gems/rspec-1.2.6/lib/spec/runner /example_group_runner.rb:15:in `load_files' 
        from /opt/local/lib/ruby/gems/1.8/gems/rspec-1.2.6/lib/spec/runner /example_group_runner.rb:14:in `each' 
        from /opt/local/lib/ruby/gems/1.8/gems/rspec-1.2.6/lib/spec/runner /example_group_runner.rb:14:in `load_files' 
        from /opt/local/lib/ruby/gems/1.8/gems/rspec-1.2.6/lib/spec/runner/options.rb:99:in `run_examples'

        from /opt/local/lib/ruby/gems/1.8/gems/rspec-1.2.6/lib/spec/runner /command_line.rb:9:in `run' 
        from /opt/local/lib/ruby/gems/1.8/gems/rspec-1.2.6/bin/spec:4
    rake aborted!
    Command /opt/local/bin/ruby
-I"/opt/local/lib/ruby/gems/1.8/gems/rspec-1.2.6/lib"
"/opt/local/lib/ruby/gems/1.8/gems/rspec-1.2.6/bin/spec"
"spec/models/post_spec.rb" "spec/requests/posts_spec.rb" --options
./spec/spec.opts failed


(See full trace by running task with --trace)

Hmm... There seem to be a lot of works to do.

Snap out of it. In accordance with the instruction of the tutorial, I did the following command,

$ merb-gen resource comment post_id:integer,name:string,body:string

and I fixed config/router.rb.

diff --git a/config/router.rb b/config/router.rb
index 173d73c..360618d 100644
--- a/config/router.rb
+++ b/config/router.rb
@@ -27,8 +27,9 @@

 Merb.logger.info("Compiling routes...")
 Merb::Router.prepare do
-  resources :comments
-  resources :posts
+  resources :posts do |p|
+    p.resources :comments
+  end
   # RESTful routes
   # resources :posts

@@ -43,4 +44,4 @@ Merb::Router.prepare do

   # Change this for your home page to be available at /
   # match('/').to(:controller => 'whatever', :action =>'index')
-end
\ No newline at end of file
+end

Then I set up the relationships, controller and view. This diff is very long, so I posted the diff to gist.

http://gist.github.com/117793

Let's run!

$ rake db:automigrate
$ merb
$ open http://localhost:4000

merb

merb2

Cool!

But when I posted a comment to a post, an error occured.

merb3

Something is wrong. Action '1' was not found in Posts... At first I suspected that config/routes.rb was wrong, but finally I found the cause is in app/views/comments/_comment.html.haml. Following patch fixes the problem.

diff --git a/app/views/comments/_comment.html.haml b/app/views/comments/_comment.html.haml 
index 7e54ce8..fcebb73 100644
--- a/app/views/comments/_comment.html.haml
+++ b/app/views/comments/_comment.html.haml
@@ -1,6 +1,6 @@
 %p= error_messages_for :comment

-= form_for(:comment, :action => "/posts/#{@post.id}/comments") do
+= form_for(:comment, :action => "/posts/#{@post.id}/comments", :method => :post) do 
   %p= text_field :name, :name => "comment[name]"
   %p= text_area  :body
   %p= submit "Comment"

The cause was the HTTP METHOD was put for some reason instead of desired post. By specifying post explicitly, the problem was solved.

I fixed the turorial wiki, so a person who tries it will never meet the problem aftertime.

Deploy

I deployed the sample merb application.

http://67.23.4.19:20090525/posts

The post number 20090525 is today's date.

This sample app server is already stopped.

No comments:

Post a Comment

Followers