我正在开发一个用于练习的应用程序(Instagram副本),我真的很难获得一个功能。
我想发生的事情是,用户可以编辑或删除自己对图片的评论。我可以使用删除功能,但无法弄清楚“编辑评论”功能。我希望用户能够在图片展示页面中编辑评论。代码在下面。
pics_controller.rb
class PicsController < ApplicationController
before_action :find_pic, only: [:show, :edit, :update, :destroy, :upvote]
before_action :authenticate_user!, except: [:index, :show]
before_action :require_same_user, only: [:edit, :update, :destroy]
def index
@pics = Pic.all.order("created_at DESC")
end
def show
end
def new
@pic = current_user.pics.build
end
def create
@pic = current_user.pics.build(pic_params)
if @pic.save
redirect_to @pic, notice: "Your pic has been posted!"
else
render :new
end
end
def edit
end
def update
if @pic.update(pic_params)
redirect_to @pic, notice: "Awesome! Your Pic was updated!"
else
render :edit
end
end
def destroy
if @pic.destroy
redirect_to root_path
end
end
def upvote
@pic.upvote_by current_user
redirect_back fallback_location: root_path
end
private
def pic_params
params.require(:pic).permit(:title, :description, :image)
end
def find_pic
@pic = Pic.find(params[:id])
end
def require_same_user
if current_user != @pic.user
flash[:danger] = "You can only edit or delete your own pictures"
redirect_to root_path
end
end
end
comments_controller.rb
class CommentsController < ApplicationController
def create
@pic = Pic.find(params[:pic_id])
@comment = @pic.comments.create(params[:comment].permit(:name, :body))
redirect_to pic_path(@pic)
end
def edit
@pic = Pic.find(params[:pic_id])
@comment = @pic.comments.find(params[:id])
redirect_to @pic
end
def update
@comment = @pic.comments.find(params[:id])
@comment.update_attributes(comment_params)
if @comment.save
redirect_to @pic
end
end
def destroy
@pic = Pic.find(params[:pic_id])
@comment = @pic.comments.find(params[:id])
@comment.destroy
redirect_to pic_path(@pic)
end
def show
@pic = Pic.find(params[:pic_id])
end
private
def comment_params
params.require(:comment).permit(:body)
end
end
这是从显示页面调用的(_comment.html.erb)部分
<div class="card" style="width: 18rem;">
<div class="card-header">
<span class="badge badge-dark"><%= comment.name %></span>
</div>
<ul class="list-group list-group-flush">
<p><%= comment.body %></p>
</ul>
</div>
<% if user_signed_in? && comment[:body].present? %>
<p><%= link_to 'Delete Comment', [comment.pic, comment], method: :delete, class: "btn btn-danger",
data: { confirm: "Are you sure?" } %></p>
<p><%= link_to 'Edit Comment', edit_pic_comment_url(@pic, comment), class: 'btn btn-primary' %></p>
<% end %>
任何帮助将不胜感激。 TIA
答案 0 :(得分:0)
您需要针对这种情况进行拒绝。步骤将是:
data-remote=true
选项创建编辑链接。它将使链接能够以ajax请求在服务器上命中。另外,添加可编辑的div
。 <a href="<%= edit_pic_comment_url(@pic, comment) %>" data-remote="true">Edit</a>
<div id="comment_#{comment.id}_form"></div>
在编辑方法中,使用js
响应请求。喜欢:
def edit
respond_to do |format|
format.js # actually means: if the client ask for js -> return edit.js
end
end
创建edit.js
文件。在此处,在ID为_form.html.erb
的已定义div上呈现评论comment_4_form
(假设您现在正在编辑ID为4的评论)。
我不详细说明。如果我详细说明,解决方案将更大。但是,如果您了解周期,那您就很好了。
答案 1 :(得分:0)
您可以使用(jquery和ajax)
检查Railscasts教程第136集Railscasts