Ruby on Rails Friday, February 9, 2018

Can someone tell me how this works, I just don't understand what's going on here

_form.html.erb
<%= form_for [post, comment], remote: true,
  html: {
    class: "new_blog_comment", id: "new_blog_comment" } do |f| -%>

  <p><%=t :leave_a_comment, scope: 'blogit.comments'%></p>

  <%= field do %>
    <%= f.label :body, t(:your_comment, scope: 'blogit.comments') %><br>
    <%= f.text_area :body %><br>
    <%= errors_on(comment, :body) %>
  <% end %>

  <%= actions do %>
    <%= f.submit t(:add_comment, scope: 'blogit.comments'), :disable_with => t(:adding_comment, scope: 'blogit.comments') %>
  <% end %>
<% end -%>


  before_action :find_commentable, only: :create

 def create
    commentable = commentable_type.constantize.find(commentable_id)
    @comment = Comment.build_from(commentable, current_user.id, body)
    user_id = commentable.user_id

    respond_to do |format|
      if @comment.save
        make_child_comment
        format.html  { redirect_to("/page/#{user_id}", :notice => 'Comment was successfully added.') }
      else
        format.html  { render :action => "new" }
      end
    end
  end

  def find_commentable
    @commentable_type = params[:commentable_type].classify
    @commentable = @commentable_type.constantize.find(params[:commentable_id])
  end

create.js.erb

var $form = $("form#new_blog_comment");
<% if @comment.save %>
  $("#comments").append("<%= escape_javascript(render(@comment)) %>");
  $form.get(0).reset();
<% else %>
  $form.html("<%= escape_javascript(render('form')) %>");
<% end %>

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/fedf8f28-d7ec-4e3f-bf5c-e6be29013781%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment