Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- controllers\comments_controller.rb
- class CommentsController < InheritedResources::Base
- def create
- @commentable = find_commentable
- @comment = @commentable.comments.build(params[:comment])
- @comment.user_id = current_user.id
- if @comment.save
- respond_to do |format|
- format.js
- end
- #redirect_to url_for(@commentable)
- else
- render :controller => @commentable.class.downcase.pluralize, :action => :show
- end
- end
- def find_commentable
- params.each do |name, value|
- if name =~ /(.+)_id$/
- return $1.classify.constantize.find(value)
- end
- end
- nil
- end
- def destroy
- @commentable = find_commentable
- @comment = Comment.find(params[:id])
- @comment.destroy
- respond_to do |format|
- format.js
- end
- #redirect_to url_for(@commentable)
- end
- end
- views/comments/_form.html.haml
- = semantic_form_for [@commentable, @comment], :remote => true do |f|
- = f.inputs do
- = f.input :content, :as => :text, :input_html => { :rows => 5, :cols => 30 }
- = f.buttons do
- = f.commit_button :label => "Отправить"
- views\comments\_comment.html.haml
- %li.comment{ :id => "comment#{comment.id}"}
- = comment.user.username
- = simple_format comment.content
- - if current_user.id == comment.user_id
- = link_to "remove", [@commentable, @comment], :method => :delete, :remote => true, :class => :delete
- views\comments\create.js.erb
- $("#comment").append("<%= escape_javascript(render(@comment)) %>")
- views\comments\destroy
- $("#comment<%= @comment.id %>").fadeOut();
- views\photos\show.html.haml
- = image_tag (@photo.store_url)
- #comment
- %ul
- - for comment in @photo.comments
- %li.comment{ :id => "comment#{comment.id}"}
- = comment.user.username
- = simple_format comment.content
- - if current_user.id == comment.user_id
- = link_to "remove", photo_comment_path(@photo.id, comment), :method => :delete, :remote => true, :class => :deletete
- = semantic_form_for [@commentable, @comment], :remote => true do |f|
- = f.inputs do
- = f.input :content, :as => :text, :input_html => { :rows => 5, :cols => 30 }
- = f.buttons do
- = f.commit_button :label => "Отправить"
- #prev
- = link_to("Previous", @photo.album.previous_photo( @photo.album.photos.index(@photo) ))
- #next
- = link_to("Next", @photo.album.next_photo( @photo.album.photos.index(@photo) ))
- #back
- = link_to "Back To Album", user_album_path(@photo.album.user_id, @photo.album_id)
- controllers\photos_controller.rb
- class PhotosController < StoresController
- before_filter :fetch_photo, :only=> [:show]
- def new
- @photo = Photo.new(:album_id => params[:album_id])
- end
- def create
- @photo = Photo.new(params[:photo])
- if @photo.save
- flash[:notice] = "Successfully created file."
- redirect_to user_album_path(@photo.album.user_id, @photo.album_id)
- else
- render :action => 'new'
- end
- end
- def edit
- @photo = Photo.find(params[:id])
- end
- def update
- @photo= Photo.find(params[:id])
- if @photo.update_attributes(params[:photo])
- flash[:notice] = "Successfully updated file."
- redirect_to user_album_path(@photo.album.user_id, @photo.album_id)
- else
- render :action => 'edit'
- end
- end
- def destroy
- @photo = Photo.find(params[:id])
- @photo.destroy
- flash[:notice] = "Successfully destroyed file."
- redirect_to :back
- end
- def show
- @photo = Photo.find(params[:id])
- @comment = @commentable.comments.build
- end
- def fetch_photo
- @photo = @commentable = Photo.find(params[:id])
- end
- end
Add Comment
Please, Sign In to add comment