[英]rails 5 undefine template error
i am creating rails 5 and adding comment to a show action which is displayed in modal 我正在创建rails 5并向以模态显示的show action添加注释
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 在我尝试通过ajax注入新的commect之前,所有这些方法都可以正常工作
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... 如果您使用locals: ...
还必须使用partial:
:,或者在这种情况下都省略两者...
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 首先,我将creat.js.erb清理为
$("#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 secondy正在获取错误nil类,因为我没有在我的注释控制器中使用即时变量
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 : 然后在您的视图中,您应该拥有包含您的js的文件comment / create.js.erb:
addCommentToSelfie("<%= j render 'selfies/comments/comment', comment: @comment %>");
And now @comment should exist. 现在@comment应该存在。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.