简体   繁体   中英

rails 5 undefine template error

i am creating rails 5 and adding comment to a show action which is displayed in modal

in my show action for comment i have it like this

    @selfie = Selfy.find(params[:id])
    respond_to do |format|
        format.js
    end

with this i cant get the show through modal like this

<%= link_to fetch_selfy_path(selfie.id), class: "show_lightbox", data: { featherlight: "mylightbox" }, remote: true do %>

      <img class="card-main-image" src="<%= selfie.photo.url if selfie.photo.url  %>" alt="Image Alt text">
    <% end %>
<div class="lightbox" id="lightbox">
    <%=render partial: "selfies/show", locals: { selfie: selfie }  %>
</div>

after clicking on the button we show action together with a comment

  <% selfie.comments.each do |comment| %>
              <%= render partial: "selfies/comments/comment",  locals: { comment: comment } %>
            <% end %>

where the partial looks like

  <p> <b><%= comment.user.username %>: </b><%= comment.body %></p>

all this works fine until i try to inject the new commect through ajax

addCommentToSelfie("<%= j render "selfies/comments/comment", locals: { comment: @comment } %>");

this returns and error of

ActionView::Template::Error (undefined local variable or method `comment' for #<#<Class:0x007f207400c648>:0x00557937265830>):
    1: 
    2:   <p> <b><%= comment.user.username %>: </b><%= comment.body %></p>

app/views/selfies/comments/_comment.html.erb:2:in `_app_views_selfies_comments__comment_html_erb__4557429192479440105_46989553619000'

i tried different methond but still getting same error

You're mixing up different syntaxes with some mixing up of quotes too. If you use locals: ... you must also use partial: , or omit both in this case...

addCommentToSelfie("<%= j render 'selfies/comments/comment', comment: @comment %>");

based on the answers provide above, i was able to solve my issue

first i clean my creat.js.erb to

$("#comments").append("<%= j render partial:  'selfies/comments/comment', locals: { comment: @comment } %>");

secondy was getting error nil class because i wasnt using instant variable in my comments controller

from:

def create
  comment =  @selfie.comments.new(comment_params)
  comment.user = current_user
  comment.save

  end

TO:

def create
  @comment =  @selfie.comments.new(comment_params)
  @comment.user = current_user
  @comment.save
  respond_to do |format|
      format.js
  end

from there everything works smoothly

Could you show us the action create in the comment controller ? Usually, I do something like that.

  def create
    @comment = @selfie.comments.new(comment_params)
    @comment.user = current_user
    respond_to do |format|
      if @comment.save
        format.html { redirect_to @comment }
        format.js
      else
        render :new
      end
    end
  end

Then in your view, you should have the file comments/create.js.erb that contains your js :

addCommentToSelfie("<%= j render 'selfies/comments/comment', comment: @comment %>");

And now @comment should exist.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM