Greate episode! If I should get this episode a couple of years ago, that may should saved my life jajaja!
So, this is the same logic you are using here in your post and epdisode comments?
Yep, pretty much.
Awesome! Thanks for sharing Chris!
For some reason...you often create episodes just fitting in my current needs..thanks.
Chris as part of the next part2 series, please add the ability to dynamically update without refreshing the page.... Best practice. As i am able to use the reply system, but i hate how the page has to refresh and i have been struggling to have it appear directly on the page without refreshing... That will be helpful!!
any help on this topic will be helpful
I can cover that in a follow-up episode. 👍
Thank you chris for the consideration!!! that why i re-joined:) when you cather to all your user needs, you build a loyal group of people... Thank i am looking forward for the episode... you know when that will be? :) excited!!!
hi chris,
using the rails generate command below:
rails g model Comment user:references commentable:references{ploymorphic}:index parent_id:integer body:text
created the migration file:
class CreateComments < ActiveRecord::Migration[5.2]
def change
create_table :comments do |t|
t.references :user, foreign_key: true
t.references :commentable, ploymorphic: true, foreign_key: true
t.integer :parent_id
t.text :body
t.timestamps end
end
end
which failed " PG::UndefinedTable: ERROR: relation "commentables" does not exist "
i went through the github files and found that your migration file did not have the foreign_key: true on the commentable line. i reran the migration and it worked, but just out of curiosity why did the references create a foreign_key: true for my migration and not your migration.
cheers
gus
It looks like you spelled polymorphic wrong which might be why it threw that error.
bugger! typing on the train LOL! thanks
chris can we also cover auto scrolling when comment meets at end of page, if it has more comments that it will load as user scrolls? maybe on the main page or do it on a pop up when a user clocks view more comments it pops up and then they can see all the comments and as they reach bottom if there is more comment it scrolls?
Chris for some reason, i can't get stimulus Reply effect to work.... I am able to hide the reply but clicking on it does not do anything .. it does not expand or collapse
This seems to work for me "visibility: hidden" instead of d-none
but when i click reply, that does not work. i stimulus js dont seem to work for me. any debugging tips?
I'd just throw some console.log
s into your JS and see what's getting called and what's not. Then you can figure out what you are missing.
Hi Chris, would it be worth using the Ancestry gem for nested comments like this?
I've been questioning whether there would be any noticable benefits. Thanks for this!
Yep, you certainly could. If you need those features it'd be great. Otherwise, there's not a whole lot of difference.
Hello Christ
I'm using the simplemde editor on my rails 5.0 blog site. simplemde displayed fine under the comment until i added nested comments based on this tutorial. The problem now is only the first reply textarea has simplemde attached to it correctly. but after submiting a first comment when i click reply i just get a basic textarea without simplemde attached to it.
I'm trying to achieve exactly how simplemde is being used with nested comments tread here at goRails
This is what my posts.coffee look like
simplemde = null cleanupSimpleMDE = -> if simplemde? simplemde.toTextArea() simplemde = null $(window).on 'popstate', cleanupSimpleMDE $(document).on 'turbolinks:before-visit', cleanupSimpleMDE $(document).on 'turbolinks:load', -> $mde = $(".simplemde") QUEUE = MathJax.Hub.queue if $mde.length > 0 $mde.each (_, el) -> simplemde = new SimpleMDE({ element: el, ...... })
and my form partial looks looks like this
<%=form_for([commentable, Comment.new], local: true, html: {class: local_assigns[:class], data: {target: local_assigns[:target]}}) do |form| %> <p class="message-area"><%=form.text_area :content, class: "simplemde", id: "md-editor"%></p> <%=form.hidden_field :parent_id, value: local_assigns[:parent_id]%> <p><%=form.error_message_for(:content)%></p> <div class=""> <span class="inline-block p-r-10"> <%=form.submit "Post reply", class: "btn btn-primary", id: "submit_comment"%> </span> </div> <% end %>
Is there something i'm missing? Thanks for your help
Hey Lunik,
It sounds like SimpleMDE is not getting attached to the hidden reply comment forms.
For GoRails, I had to do this, so it applies SimpleMDE to any visible forms and then it also applied it when you click the Reply link.
Apologizes for the messy code, this could use some refactoring. :)
simplemde = [] cleanupSimpleMDE = -> # Clean up if already exists if simplemde.length > 0 for editor in simplemde editor.toTextArea() simplemde = [] addSimpleMDE = (field) -> editor = new SimpleMDE({ autoDownloadFontAwesome: false, element: field, toolbar: ["bold", "italic", "heading", "|", "code", "quote", "unordered-list", "ordered-list", "clean-block", "|", "link", "image", "|", "preview", "side-by-side", "fullscreen", "guide"], spellChecker: false status: false }) simplemde.push(editor) return editor @setupEditors = -> for editor in $(".simplemde:visible") addSimpleMDE(editor) $("[data-behavior='show-comment-form']").on "click", (e) -> e.preventDefault() reply_link = $(this) reply_link.hide() form = reply_link.parents(".forum_post").first().find("[data-behavior='comment-form']") form.show() addSimpleMDE(form.find("textarea")[0]) $(window).on 'popstate', cleanupSimpleMDE $(document).on 'turbolinks:before-visit', cleanupSimpleMDE $(document).on 'turbolinks:load', @setupEditors
Rather than passing in commentable
and parent_id
locals to the comments/form
partial I tend to build the new Comment object, and pass that through instead. Like this:
<%= render partial: "comments/form", locals: { comment: Comment.new(commentable: @post, parent_id: comment.id) } %>
This way the commentable
and parent_id
values are always shown in their context (a Comment
object), rather than randomly floating around in your views.
I'm curious what you think of this approach, as it doesn't just apply to this particular video but Rails views in general, specifically forms.
I don't know much about how Rails templating works under the hood to say whether this has any impact on performance, etc.
I have nested routes so everytime I try to render the form I get an error.
How should I change the form to work with nested routes? This line throws an error, since it can't find tasks_comments_path
<%= form_with model: [commentable, Comment.new], local: true do |form| %>
My routes look like this:
resources :projects do resources :project_steps, path: "step", only: [:show, :update] resources :tasks do resources :comments, module: :tasks end resources :reviews, only: [:create, :destroy] end
Hey Arnas,
Just make your form match your routes like you normally would:
<%= form_with model: [@project, @task, Comment.new], local: true do |form| %>
Thank you.
This worked, but I'm still facing an issue with the Tasks::CommentsController, since I can't use Task.friendly.find(params[:task_id])
.
My method currently looks like this, but it throws an error, that it can't find the id when using params[:id]
and when I use params[:task_id]
, I get the error undefined method task_url for Class.......
This is the method I'm using:
def set_commentable @project = Project.friendly.find(params[:project_id]) @tasks = @project.tasks.order(:tag) @commentable = @tasks.friendly.find(params[:task_id]) end
Great tutorial.
One thing, if you're using rich_text instead of normal text fields for the comment body, then when rendering you'll want to add the id to the render, or else all of the rich_text editors (event the nested ones) will have the same id, and nothing will save.
for app/views/comments/_form.html.erb
<%= form_with model: [commentable, Comment.new], local: true, html: { class: local_assigns[:class], data: { target: local_assigns[:target] } } do |form| %> <div class="form-group"> <%= form.rich_text_area :body, id: local_assigns[:parent_id] %> </div> <div class="form-group"> <%= form.hidden_field :parent_id, value: local_assigns[:parent_id] %> <%= form.submit class: "btn btn-primary" %> </div> <% end %>
Join 27,623+ developers who get early access to new screencasts, articles, guides, updates, and more.