Art*_*iro 51 ruby ruby-on-rails hyperlink
所以,假设我有帖子和评论,而节目的网址是/posts/1/comments/1
.我想在comments controller destroy方法中创建一个删除该注释的链接.我怎么做?
Ian*_*hop 107
<%= link_to 'Destroy', post_comment_path(@post, comment),
data: {:confirm => 'Are you sure?'}, :method => :delete %>
Run Code Online (Sandbox Code Playgroud)
在评论控制器中:
def destroy
@post = Post.find(params[:post_id])
@comment = Comment.find(params[:id])
@comment.destroy
respond_to do |format|
format.html { redirect_to post_comments_path(@post) }
format.xml { head :ok }
end
end
Run Code Online (Sandbox Code Playgroud)
从一段时间以前,该confirm
选项必须包含在data
哈希中,否则将被忽略:
<%= link_to 'Destroy', post_comment_path(@post, comment),
data: { confirm: 'Are you sure?' }, method: :delete %>
Run Code Online (Sandbox Code Playgroud)
@post
鉴于您的帖子对象有变量
<%= link_to 'Delete comment', post_comment_path(@post, comment),
method: :delete, data: {confirm: 'Are you sure?'} %>
Run Code Online (Sandbox Code Playgroud)
<%= link_to 'Delete comment', post_comment_path(@post, comment),
data: {turbo_method: :delete, turbo_confirm: 'Are you sure?'} %>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
43263 次 |
最近记录: |