ruby - Correct way to add a comments to existing view rails -
i have rails application wih scaffold displays images via controllers show action. want add comments every picture. right way this? tried making second controller+model+view, rendered comments form partial in images show view , passed image id via parameter. works, don't think how it's supposed done. if know example project implements aomething please send me, couldn't find anything. thank help.
this typically handled nested resources:
#config/routes.rb resources :images #-> url.com/images/:id resources :comments, only: [:create, :update, :destroy] #-> url.com/images/:image_id/comments end #app/controllers/images_controller.rb class imagescontroller < applicationcontroller def show @image = image.find params[:id] @comment = @image.comments.new end end #app/controllers/comments_controller.rb class commentscontroller < applicationcontroller def create @image = image.find params[:image_id] @comment = @image.comments.new comment_params redirect_to @image if @comment.save end private def comment_params params.require(:comment).permit(:body).merge(user_id: current_user.id) end end
you'll able show views follows:
#app/views/images/show.html.erb <%= @image.attribute %> <%= form_for [@image, @comment] |f| %> <%= f.text_field :body %> <%= f.submit %> <% end %>
of course, put partial
. there number of ways working, above way i'd handle it.
Comments
Post a Comment