Rails 4表单部分不立即呈现提交的数据

时间:2015-08-13 21:54:41

标签: ruby-on-rails forms

可能是一个我忽略的非常简单的问题。我正在构建一个类似于Facebook" home"的功能。登录用户的页面。用户可以在一个表单中发布主题,并且该表单可以完美地运行。

每个发布的主题下都有一个评论表单。当用户输入注释并单击提交按钮时,将创建注释,但除非我手动刷新页面,否则不会显示注释。我无法看到我在这里做错了什么。

_form.html.haml

= form_for [topic, Comment.new], remote: true do |f|
  .form-group
    = f.text_area :body, rows: 2, class: 'form-control', placeholder: "Make a comment"
    = f.submit "Post", class: 'f-button primary f-fw-bold post-btn'

我也试过在这个表单中使用@topic但是得到错误:未定义的方法`comments_path'

comments_controller.rb

class CommentsController < ApplicationController
  def create
    puts "TOPICS PARAMS",params[:topic_id]
    @topic = Topic.find(params[:topic_id])
    @comments = @topic.comments

    @comment = current_user.comments.build( comment_params )
    @comment.topic = @topic
    @new_comment = Comment.new

    if @comment.save
      flash[:notice] = "Comment was created."
      redirect_to topics_path
    else
      flash[:error] = "There was an error saving the comment. Please try again."
      redirect_to topics_path
    end
  end

  private
  def comment_params
    params.require(:comment).permit(:body, :topic_id)
  end
end

所有这些都在主题#index path中呈现,所以这里也是主题控制器。

topics_controller.rb

class TopicsController < ApplicationController
  def index
    @topics = Topic.order(created_at: :desc)
    @comments = Comment.all
    @limited_partners = LimitedPartner.all
    @users = User.all
    @comment = Comment.new
  end

  def show
    @topic = Topic.find(params[:id])
  end

  def create
    @topic = Topic.new(topic_params)
    @topic.user_id = current_user.id if current_user
    @topic.limited_partner_id = current_user.limited_partner_id if current_user
    if @topic.save
      flash[:notice] = "Topic was saved successfully."
      redirect_to topics_path
    else
      flash[:error] = "Error creating topic. Please try again."
      render :new
    end
  end

  def new
  end

  def edit
  end

  def update
  end

  private
  def topic_params
    params.require(:topic).permit(:body, :liked, :limited_partner_id, :user_id, :comment_id)
  end
end

在index.html.haml文件中,我像这样调用部分:

= render partial: 'comments/form', locals: { topic: topic, comment: @comment} 

3 个答案:

答案 0 :(得分:1)

你的问题可能就在这里......

= form_for [topic, Comment.new], remote: true do |f|

试试这个

= form_for @new_comment, url: {controller: 'comments', action: 'create'}, method: "post", remote: true do

并确保您的config / routes.rb看起来像这样

get "/some-path", to: "comments#create"
post "/some-path", to: "comments#create"

答案 1 :(得分:1)

您正在使用@"OK"表单。因此,提交将触发Ajax请求。将返回javascript响应,但默认情况下不会更新任何HTML。

您需要使用一些javascript来自行更新HTML:bind a callback to the ajax:success eventuse a js view(例如remote: true)。

还要查看Turbolinks 3(仍处于开发阶段),这可以减少部分页面更新所需的自定义JavaScript数量。

答案 2 :(得分:0)

我不得不使用javascript来更好地控制表单和数据。所以我用这个创建了一个topic.coffee文件:

$ ->
  $('.new_comment').on 'submit', (event) =>
    form = $(event.target).closest('form')
    topicCommentsId = form.attr('action').replace(/\//g, '_').substring(1)
    owningCommentsSection = $('#' + topicCommentsId)
    formData = form.serialize()
    $.post form.attr('action'), formData, (data) =>
      extractedBody = $(data.substring(data.indexOf('<body')))
      topicComments = extractedBody.find('#' + topicCommentsId)
      owningCommentsSection.html(topicComments.html())
      form.find('[name="comment[body]"]').val('')
      location.reload();
    return false

我也从我的表单中删除了remote:true并使用以下内容识别index.html.haml中的每个主题:

.f-grid-row.topic_comments{id: "topics_#{topic.id}_comments"}
    - topic.comments.each do |comment|
        - if comment.topic_id == topic.id || comment.post_id == topic.id
            ...