Цей туторіал покаже як створити простеньку аплікацію на .
gem install mongrel
gem install merb
$ sudo gem install merb
gem install do_sqlite3-0.9.10.1-x86-mswin32.gem
merb-gen app blog --template-engine=haml
cd blog
merb-gen resource post title:string,body:string
merb-gen resource comment post_id:integer,name:string,body:string
Merb::Router.prepare do
resources :comments
resources :posts
...
end
Merb::Router.prepare do
resources :posts do |post|
post.resources :comments
end
...
end
class Post
include DataMapper::Resource
property :id, Serial
property :body, String
property :title, String
has n, :comments
end
class Comment
include DataMapper::Resource
property :id, Serial
property :name, String
property :body, String
property :post_id, Integer
belongs_to :post
end
= error_messages_for :post
= form_for(@post, :action => url(:posts) ) do
= partial :form
%p= submit "Create"
= link_to 'Back', url(:posts)
= error_messages_for :post
= form_for(@post, :action => url(:post, @post)) do
= partial :form
%p= submit "Update"
= link_to 'Show', url(:post, @post)
|
= link_to 'Back', url(:post)
%p= text_field :title
%p= text_area :body
%table
%tr
- for post in @posts
%td= post.title
%tr
%td= link_to 'Show', url(:post, post)
%td= link_to 'Edit', url(:edit_post, post)
= link_to 'New', url(:new_post)
%p= @post.title
%p= @post.body
%br/
%hr/
%h1 Comments
%p= partial "comments/show", :with => @post.comments.reverse, :as => :comment
%hr/
%p= partial("comments/comment")
= link_to 'Edit', url(:edit_post, @post)
|
= link_to 'Back', url(:posts)
%p= comment.name
%p= comment.body
%p= error_messages_for :comment
= form_for(:comment, :action => "/posts/#{@post.id}/comments") do
%p= text_field :name, :name => "comment[name]"
%p= text_area :body
%p= submit "Comment"
def create(comment)
@post = Post.get(params[:post_id])
@comment = Comment.new(comment.merge(:post => @post))
if @comment.save
redirect resource(@post), :message => {:notice => "Comment was successfully created"}
else
message[:error] = "Comment failed to be created"
render :new
end
end
rake db:automigrate
merb
|
mux |
Коментарі (3)
RSS згорнути / розгорнутиblaster
mux
Niko
Тільки зареєстровані й авторизовані користувачі можуть залишати коментарі.