Guest User

Untitled

a guest
Jan 12th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.64 KB | None | 0 0
  1. controllers\comments_controller.rb
  2.  
  3. class CommentsController < InheritedResources::Base
  4.  
  5. def create
  6. @commentable = find_commentable
  7. @comment = @commentable.comments.build(params[:comment])
  8. @comment.user_id = current_user.id
  9.  
  10. if @comment.save
  11. respond_to do |format|
  12. format.js
  13. end
  14. #redirect_to url_for(@commentable)
  15. else
  16. render :controller => @commentable.class.downcase.pluralize, :action => :show
  17. end
  18. end
  19.  
  20. def find_commentable
  21. params.each do |name, value|
  22. if name =~ /(.+)_id$/
  23. return $1.classify.constantize.find(value)
  24. end
  25. end
  26. nil
  27. end
  28.  
  29. def destroy
  30. @commentable = find_commentable
  31. @comment = Comment.find(params[:id])
  32. @comment.destroy
  33. respond_to do |format|
  34. format.js
  35. end
  36. #redirect_to url_for(@commentable)
  37. end
  38.  
  39. end
  40.  
  41. views/comments/_form.html.haml
  42. = semantic_form_for [@commentable, @comment], :remote => true do |f|
  43. = f.inputs do
  44. = f.input :content, :as => :text, :input_html => { :rows => 5, :cols => 30 }
  45. = f.buttons do
  46. = f.commit_button :label => "Отправить"
  47.  
  48. views\comments\_comment.html.haml
  49. %li.comment{ :id => "comment#{comment.id}"}
  50. = comment.user.username
  51. = simple_format comment.content
  52. - if current_user.id == comment.user_id
  53. = link_to "remove", [@commentable, @comment], :method => :delete, :remote => true, :class => :delete
  54.  
  55. views\comments\create.js.erb
  56. $("#comment").append("<%= escape_javascript(render(@comment)) %>")
  57.  
  58. views\comments\destroy
  59. $("#comment<%= @comment.id %>").fadeOut();
  60.  
  61. views\photos\show.html.haml
  62. = image_tag (@photo.store_url)
  63.  
  64. #comment
  65. %ul
  66. - for comment in @photo.comments
  67. %li.comment{ :id => "comment#{comment.id}"}
  68. = comment.user.username
  69. = simple_format comment.content
  70. - if current_user.id == comment.user_id
  71. = link_to "remove", photo_comment_path(@photo.id, comment), :method => :delete, :remote => true, :class => :deletete
  72.  
  73. = semantic_form_for [@commentable, @comment], :remote => true do |f|
  74. = f.inputs do
  75. = f.input :content, :as => :text, :input_html => { :rows => 5, :cols => 30 }
  76. = f.buttons do
  77. = f.commit_button :label => "Отправить"
  78.  
  79. #prev
  80. = link_to("Previous", @photo.album.previous_photo( @photo.album.photos.index(@photo) ))
  81. #next
  82. = link_to("Next", @photo.album.next_photo( @photo.album.photos.index(@photo) ))
  83.  
  84. #back
  85. = link_to "Back To Album", user_album_path(@photo.album.user_id, @photo.album_id)
  86.  
  87. controllers\photos_controller.rb
  88. class PhotosController < StoresController
  89.  
  90. before_filter :fetch_photo, :only=> [:show]
  91.  
  92. def new
  93. @photo = Photo.new(:album_id => params[:album_id])
  94. end
  95.  
  96. def create
  97. @photo = Photo.new(params[:photo])
  98. if @photo.save
  99. flash[:notice] = "Successfully created file."
  100. redirect_to user_album_path(@photo.album.user_id, @photo.album_id)
  101. else
  102. render :action => 'new'
  103. end
  104. end
  105.  
  106. def edit
  107. @photo = Photo.find(params[:id])
  108. end
  109.  
  110. def update
  111. @photo= Photo.find(params[:id])
  112. if @photo.update_attributes(params[:photo])
  113. flash[:notice] = "Successfully updated file."
  114. redirect_to user_album_path(@photo.album.user_id, @photo.album_id)
  115. else
  116. render :action => 'edit'
  117. end
  118. end
  119.  
  120. def destroy
  121. @photo = Photo.find(params[:id])
  122. @photo.destroy
  123. flash[:notice] = "Successfully destroyed file."
  124. redirect_to :back
  125. end
  126.  
  127. def show
  128. @photo = Photo.find(params[:id])
  129. @comment = @commentable.comments.build
  130. end
  131.  
  132. def fetch_photo
  133. @photo = @commentable = Photo.find(params[:id])
  134. end
  135. end
Add Comment
Please, Sign In to add comment