[英]Rails 5.0 - Comments not working
我正在将评论作为事件应用程序的嵌套资源来实现,并且一个接一个地发布。 最初它运行良好,但是,它们仅有的功能是创建和销毁。 我想使用Ajax / remote添加编辑功能:对于同一页面编辑(以前从未做过)为true,但是我碰到了墙。 编辑link_to无法/从未使用过,现在甚至create函数也无法使用。 这就是开发日志中正在发生的事情-
Processing by CommentsController#create as JS
Parameters: {"utf8"=>"✓", "comment"=>{"body"=>"Comment."}, "commit"=>"Create Comment", "event_id"=>"27"}
[1m[36mComment Load (0.1ms)[0m [1m[34mSELECT "comments".* FROM "comments" WHERE "comments"."id" = ? LIMIT ?[0m [["id", nil], ["LIMIT", 1]]
Completed 404 Not Found in 1ms (ActiveRecord: 0.1ms)
ActiveRecord::RecordNotFound (Couldn't find Comment with 'id'=):
我通过反复试验尝试了各种不同的参数,但“ id”问题不断涌现。 这是我的代码-
comments_controller.rb
class CommentsController < ApplicationController
before_action :set_comment, only: [:show, :create, :edit, :update, :destroy]
def create
@event = Event.find(params[:event_id])
@comment = @event.comments.create(comment_params)
@comment.user_id = current_user.id
if @comment.save
redirect_to @event
else
render 'new'
end
end
# GET /comments/1/edit
def edit
@event = @comment.event
@comment = @event.comments.find(params[:id])
respond_to do |f|
f.js
f.html
end
end
def show
end
def update
if @comment.update(comment_params)
redirect_to @event, notice: "Comment was successfully updated!"
else
render 'edit'
end
end
def destroy
@event = Event.find(params[:event_id])
@comment = @event.comments.find(params[:id])
@comment.destroy
redirect_to event_path(@event)
end
private
def set_comment
@comment = Comment.find(params[:id])
end
def set_event
@event = Event.find(params[:event_id])
end
def comment_params
params.require(:comment).permit(:name, :body)
end
end
_comment.html.erb
<div class="comment clearfix">
<div class="comment_content">
<div id="<%=dom_id(comment)%>" class="comment">
<p class="comment_name"><strong><%= comment.name %></strong></p>
<p class="comment_body"><%= comment.body %></p>
</div>
<p><%= link_to 'Edit', edit_event_comment_path([comment.event, comment]), id: "comment", remote: true %></p>
<p><%= link_to 'Delete', [comment.event, comment],
method: :delete,
class: "button",
data: { confirm: 'Are you sure?' } %></p>
</div>
</div>
_form.html.erb
<%= simple_form_for([@event, @comment], remote: true) do |f| %>
<%= f.label :comment %><br>
<%= f.text_area :body %><br>
<br>
<%= f.button :submit, label: 'Add Comment', class: "btn btn-primary" %>
<% end %>
edit.js.erb
$('#comment').append('<%= j render 'form' %>');
我想我对这件事以及如何获取remote: true
函数正在页面上与'id'混为一谈。 我不想接受失败,但是如果我没有成功,我可能不得不接受。
更新-
当我尝试编辑现有注释时,我会在开发日志中得到该注释-
Started GET "/events/27%2F32/comments/27/edit" for ::1 at 2017-05-24 12:28:20 +0100
Processing by CommentsController#edit as JS
Parameters: {"event_id"=>"27/32", "id"=>"27"}
[1m[36mComment Load (0.1ms)[0m [1m[34mSELECT "comments".* FROM "comments" WHERE "comments"."id" = ? LIMIT ?[0m [["id", 27], ["LIMIT", 1]]
Completed 404 Not Found in 1ms (ActiveRecord: 0.1ms)
ActiveRecord::RecordNotFound (Couldn't find Comment with 'id'=27):
该路线没有意义- "/events/27%2F32/comments/27/edit"
-注释ID应该为32,事件ID为27。
的routes.rb
Rails.application.routes.draw do
devise_for :users, :controllers => { omniauth_callbacks: "omniauth_callbacks", registrations: "registrations" }
resources :users
# the above resource draws out routes for user profiles
resources :events do
resources :comments
resources :bookings
end
root 'events#index'
更改
before_action :set_comment, only: [:show, :create, :edit, :update, :destroy]
至
before_action :set_comment, only: [:show, :edit, :update, :destroy]
创建评论时,您无法设置评论。
另外,如评论中所述,您的编辑链接应为,
<%= link_to 'Edit', [comment.event, comment], id: "comment", remote: true %>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.