【问题标题】:rails processing as HTML instead of JSrails 处理为 HTML 而不是 JS
【发布时间】:2017-02-08 19:41:53
【问题描述】:

我正在尝试使用 ajax 创建一个函数来编辑和更新表单中的 cmets。 我的编辑功能使用 ajax 没有问题,但是当我尝试更新评论时,我收到错误:CommentsController#update 缺少此请求格式和变体的模板。 request.formats: ["text/html"] request.variant: []

cmets_controller

def update
    respond_to :js
    authorize @comment, :update?
    @comment.update(comment_params)


    if @comment.save
      flash[:notice] = 'Commentaar is succesvol toegevoegd.'
    else
      flash.now[:alert] = 'Commentaar is niet toegevoegd.'
    end      
end

def comment_params
    params.require(:comment).permit(:text)
end

update.js.erb

$("#comment-ajax-<%= @comment.id %>").html("<%= j render @comment %>");

_comment.html.erb

<% if policy(comment).edit? %>
   <%= link_to 'edit', [:edit, comment.fabmoment, comment], remote: true, 'data-type' => 'script' %>
<% end %>

意见表

<%= simple_form_for [fabmoment, comment] do |f| %>
    <!-- Input -->
    <%= f.input_field :text, rows: 4 %>
    <%= f.label :text %>
<% end %>

【问题讨论】:

  • 您还可以从视图中显示您的表单代码吗?
  • 能否也添加请求参数?

标签: ruby-on-rails ajax


【解决方案1】:

错误消息告诉您表单正在作为“text/html”提交。尝试在实际表单中添加remote: true 而不是link_to

【讨论】:

  • 非常感谢!我们已经连续解决这个问题两天了,但答案就是这么简单哈哈
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多