Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## router.rb
- Merb::Router.prepare do
- resources :comments
- resources :posts do
- member :add_comment, :method => :post
- end
- ...
- end
- ## show.html.haml
- = form_for @comment, :action => resource(@post, :add_comment), :method => :post do
- .input
- = text_field :name => "posted_by", :label => "Posted By"
- .input
- = text_field :name => "url", :label => "Website"
- .input
- = text_field :name => "email", :label => "Email"
- .input
- = text_area :name => "body", :label => "Comment"
- .input
- = submit "Post Comment"
- ## posts.rb (controller)
- class Posts < Application
- # provides :xml, :yaml, :js
- before :ensure_authenticated #, :only => [:index :edit :create :update :destroy]
- def index
- @posts = Post.all
- display @posts
- end
- def show(id)
- @post = Post.get(id)
- raise NotFound unless @post
- @comment = Comment.new
- display @post
- end
- def new
- only_provides :html
- @post = Post.new
- display @post
- end
- def edit(id)
- only_provides :html
- @post = Post.get(id)
- raise NotFound unless @post
- display @post
- end
- def create(post)
- @post = Post.new(post)
- if @post.save
- redirect resource(@post), :message => {:notice => "Post was successfully created"}
- else
- message[:error] = "Post failed to be created"
- render :new
- end
- end
- def update(id, post)
- @post = Post.get(id)
- raise NotFound unless @post
- if @post.update_attributes(post)
- redirect resource(@post)
- else
- display @post, :edit
- end
- end
- def destroy(id)
- @post = Post.get(id)
- raise NotFound unless @post
- if @post.destroy
- redirect resource(:posts)
- else
- raise InternalServerError
- end
- end
- def add_comment(id)
- @post = Post.get(id)
- raise NotFound unless @post
- =begin ## Something I was playing with
- #props = Comment.properties.map {|p| p.name}
- #c_params = params.select {|k,v| props.include?(k)}
- #raise c_params.to_yaml
- #@comment = @post.comments.build(c_params)
- =end
- #@comment = @post.comments.build(params) #=> Does not work
- @comment = @post.comments.build( # ugly but it works
- :posted_by => params[:posted_by],
- :email => params[:email],
- :body => params[:body]
- ## Other params here
- )
- if @comment.save
- message[:message] = "Comment Created"
- else
- message[:error] = "Comment failed to be created"
- end
- redirect resource(@post)
- end
- end # Posts
Add Comment
Please, Sign In to add comment