【发布时间】:2017-09-28 20:58:26
【问题描述】:
我不确定我是否正确传递了帖子,但评论仍在创建中,当我在浏览器上查看我的 index.html.erb 页面时,它工作正常。
发表评论后,我的终端中不断出现此错误。
ActionView::Template::Error (undefined local variable or method `post'
for #<#<Class:0x007fc9c544ee90>:0x007fc9c527f498>
Did you mean? @post):
1: <% post.comments.each do |comment| %>
2: <tr id="comments_<%= comment.id %>">
3: <td><p><%= comment.user.email %></p></td>
4: <td><p><%= comment.content %></p></td>
app/views/comments/_comment.html.erb:1:in `_app_views_comments__comment_html_erb__2743602892026494484_70252313407540'
app/views/posts/show.html.erb:13:in `_app_views_posts_show_html_erb__792041857476014578_70252313539340'
这是我的评论控制器
class CommentsController < ApplicationController
before_action :find_post
before_action :find_comment, only: [:destroy,:edit,:update,:comment_owner]
before_action :comment_owner, only: [:destroy,:edit, :update]
def create
@comment = @post.comments.new(comment_params)
@comment.user_id = current_user.id
@comment.save
respond_to do |format|
if @comment.save
format.html { redirect_to @post, notice: 'Comment was successfully created' }
format.json { render :show, status: :created, location: @post }
format.js
else
format.html { render :new }
format.json { render json: @post.errors, status: :unprocessable_entity }
format.js
end
end
end
def edit
end
def update
if @comment.update(params[:comment].permit(:content))
redirect_to post_path(@post)
else
render 'edit'
end
end
def destroy
@comment.destroy
redirect_to post_path(@post)
end
private
def find_post
@post = Post.find(params[:post_id])
end
def find_comment
@comment = @post.comments.find(params[:id])
end
def comment_owner
unless current_user.id == @comment.user_id
flash[:notice] = "Nice trick ;P"
redirect_to @post
end
end
def comment_params
params.require(:comment).permit(:content)
end
end
我的 index.html.erb
<tbody>
<%= render @posts%>
</tbody>
我的_post.html.erb
<tr id="<%= post.id %>">
<td><%= post.id %></td>
<td><%= post.title %></td>
<td><%= post.content %></td>
<td><%= link_to 'Show', post %></td>
<td><button type="button" class="edit_button" data-
target="#myupdatepost_<%= post.id %>">Edit</button></td>
<td><%= link_to 'Destroy', post, method: :delete, remote: true %>
</td>
</tr>
从这里我试图将帖子传递到下一个部分
<%= render partial: 'comments/comment', locals: {post: post} %>
<tr class="EP <%= post.id %>">
<%= form_for(post, :method => :put, remote: true) do |form| %>
<td class="field">
<%= form.label :title %>
<%= form.text_field :title, id: :post_title %>
</td>
<td class="field">
<%= form.label :content %>
<%= form.text_area :content, id: :post_content %>
</td>
<td class="actions">
<%= form.submit %>
</td>
<% end %>
</tr>
这里是_comment.html.erb
<% post.comments.each do |comment| %>
<tr id="comments_<%= comment.id %>">
<td><p><%= comment.user.email %></p></td>
<td><p><%= comment.content %></p></td>
<% if current_user == comment.user %>
<td><%= link_to "Delete Comment", [comment.post, comment], method:
:delete %></td>
<td><%= link_to "Edit Comment",
edit_post_comment_path(comment.post,comment) %></td>
<% end %>
</tr>
<% end %>
<%= render :partial => 'comments/form', :locals => {:post => post} %>
最后是 _form.html.erb
<tr class="Comm <%= post.id %>">
<%= form_for([post,post.comments.build], remote: true) do |f| %>
<td><%= f.text_area :content %></td>
<td><%= f.submit %></td>
<% end %>
</tr>
这是我的 create.js.erb 评论
console.log("ajax working coment created");
$("textarea#comment_content").val("");
console.log("<%= @post.title %>");
console.log("<%= @comment.content %>");
$("tbody").append("<%= j render(@comment) %>")
【问题讨论】:
-
你能显示帖子控制器吗?
标签: jquery ruby-on-rails ruby ajax erb