【问题标题】:Rails - Editing comments as a partialRails - 将评论作为部分编辑
【发布时间】:2017-07-05 14:25:10
【问题描述】:

我正在使用 Rails 构建一个事件应用程序,并且在事件显示页面的底部有一个 cmets 部分。我希望用户能够只创建/更新(编辑)/删除他们自己的 cmets,同时保留在同一显示页面上。我该怎么做呢?

我已经将一些代码放在一起,但我对 Rails 还很陌生,我的代码试图让用户离开显示页面并创建一个“cmets”显示页面,而不是仅仅在事件显示页面上编辑表单.我有正确的模型关联 has_many/belongs_to 并且我的 cmets 嵌套在我的事件路线中。到目前为止,这是我的代码 -

Comments_controller.rb

   class CommentsController < ApplicationController


def create
    @event = Event.find(params[:event_id])
    @comment = @event.comments.create(params[:comment].permit(:user_id, :body))


    redirect_to event_path(@event)
end

def show
    @comment = Comment.find(params[:id])
    @comment = @event.comments.find(params[:id])
end

def edit
    @comment.user = current_user

end

def update
    if @comment.update(comment_params)
        redirect_to event_path(@event)
    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 comment_params
        params.require(:comment).permit(:body, :event_id, :user_id)
    end
end

Event.show.erb

  # some code for Events show...

    # Comments code - 
                <% if user_signed_in?  %>

                <div id="comments">
                    <%= render 'comments/form', commentable: @event %>
                    <% if @event.comments.any? %>
                        <h2><%= @event.comments.size %> Comment</h2>
                        <%= render @event.comments %>
                    <% else %>
                    <h2>There are no comments yet</h2>
                <% end %>

                </div>  

                <% end %>

Comments._comment.html.erb

  <div class="comment clearfix">


 <div class="comment_content">
    <p class="comment_user"><strong><%= comment.user %></strong></p>
    <p class="comment_body"><%= comment.body %></p>
    <p class="comment_time"><%= time_ago_in_words(comment.created_at) %> Ago</p>
  </div>

  <% if user_signed_in? and current_user %>
    <p><%= link_to 'Delete', [comment.event, comment],
                                  method: :delete,
                                  class: "button",
                                    data: { confirm: 'Are you sure?' } %></p>
    <p><%= link_to 'Edit', [comment.event, comment] %> </p>                                 
 <% end %>

</div>

cmets_form.html.erb

  <%= simple_form_for([commentable, Comment.new ]) do |f| %>

<%= f.label :comment, label:  'Add a comment' %><br> 
<%= f.text_area :body %><br>
<br>
<%= f.button :submit, "Create", class: "btn btn-primary" %>
<% end %>

【问题讨论】:

  • cmets/form 的代码在哪里?
  • 我不会再帮你了。上次我花了很多时间在你身上,你说它对你有用,但是 0 点赞或标记为正确!
  • 您指的是哪个问题?

标签: ruby-on-rails ruby ruby-on-rails-4 model-view-controller


【解决方案1】:

要在同一页面上执行操作,您需要添加remote: true 到您的表单并通过 JS 提交。您可以在 Rails/JS 文档中阅读更多相关信息。 http://edgeguides.rubyonrails.org/working_with_javascript_in_rails.html 比,标准的rails方式是引导你到另一条路线。 要禁止这种情况,并在同一视图页面上执行所有操作,您可以使用 JS。

要将操作限制在当前用户,您需要确保评论属于 current_user。喜欢 如果 user_signed_in?和comment.user_id == current_user.id (以防万一您上面的代码不是防弹的。)

【讨论】:

    猜你喜欢
    • 2015-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-13
    • 1970-01-01
    • 2017-11-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多