我正在构建简单的网络应用,人们可以分享他们的想法/图片/等等。它只有两个控制器Post,并嵌套在它中。通常,在所有工作完美的情况下,用户可以像评论一样添加,编辑和删除帖子。我试图做的事情,并且遇到很大的麻烦,可以编辑评论而无需重定向到编辑评论视图 - 所以能够从"帖子#show"级别,与实际创建注释的方式相同。我觉得它看起来会更好......这是我的:
posts_controller.rb
class PostsController < ApplicationController
before_action :find_post, only: [:show, :edit, :update, :destroy]
def index
@posts = Post.all.order(created_at: :desc)
end
def new
@post = Post.new
end
def create
@post = Post.new(post_params)
if @post.save
redirect_to @post
else
render 'new'
end
end
def show
@comments = @post.comments
end
def edit
end
def update
if @post.update(post_params)
redirect_to @post
else
render 'edit'
end
end
def destroy
@post.destroy
redirect_to root_path
end
private
def find_post
@post = Post.find(params[:id])
end
def post_params
params.require(:post).permit(:title, :description)
end
end
comments_controller.rb
class CommentsController < ApplicationController
before_action :find_post, only: [:create, :edit, :update, :destroy]
def create
@comment = @post.comments.create(comment_params)
if @comment.save
redirect_to @post
else
render 'new'
end
end
def edit
@comment = @post.comments.find(params[:id])
end
def update
@comment = @post.comments.find(params[:id])
@comment.update_attributes(comment_params)
if @comment.save
redirect_to @post
else
render 'edit'
end
end
def destroy
@comment = @post.comments.find(params[:id])
@comment.destroy
redirect_to @post
end
private
def find_post
@post = Post.find(params[:post_id])
end
def comment_params
params.require(:comment).permit(:content)
end
end
查看show.html.haml - 发布
%h1 Show page
%h3= @post.title
%p= @post.description
=link_to "Edit memory", edit_post_path
=link_to "Delete memory", post_path, method: :delete
%h4 Share your thoughts about the memory
- @comments.each do |comment|
%p= comment.content
=link_to "Edit thought", edit_post_comment_path(@post, comment)
=link_to "Delete thought", post_comment_path(@post, comment), method: :delete
= simple_form_for([@post, @post.comments.build]) do |c|
= c.input :content
= c.submit "Share thought"
查看edit.html.haml - 评论(我想以某种方式摆脱/嵌套到上面显示的视图中)
= simple_form_for([@post, @comment]) do |c|
= c.input :content
= c.submit "Update thought"
我相信有一个简单的解决方案,但是尽管我已经阅读了很多关于可能解决方案的事实,对于像我这样的新手来说,仍然很难弄清楚应该如何编程。
答案 0 :(得分:1)
您正在寻找的术语是 就地编辑 ,或 内联编辑 < / p>
您需要form
或JQuery plugin
来撰写current_user
(假设您正在使用Devise)可编辑的任何评论。< / p>
我已做了一点(你可以免费注册here,点击&#34;个人资料&#34;然后编辑说明):
-
您想要这样做的方式就像 这样:
#app/assets/javascripts/application.js
# include x-editable scripts
$(".editable).editable([..options..]);
#app/views/posts/index.html.erb
<% @posts.each do |post| %>
<%= post.body %>
<% post.comments.each do |comment| %>
<% if comment.author == current_user %>
<%= content_tag :div, comment.body, class: "editable" %>
<% end %>
<% end %>
<% end %>
我忘记了我们如何实施所提供的示例; x-editable
现在似乎风靡一时。
以下是它的工作原理:
.class
或#id
X-Editable
使其可编辑X-Editable
然后将完成的请求发送到服务器,充当Ajax 看起来你可以使用x-editable-rails
gem:
#Gemfile
gem `x-editable-rails`
#app/assets/javascripts/application.js
#= require editable/bootstrap-editable
#= require editable/rails
#app/views/posts/index.html.erb
<% @posts.each do |post| %>
<% post.comments.each do |comment| %>
<%= editable comment, :body if comment.author == current_user %>
<% end %>
<% end %>