【问题标题】:rails ajax controller won't call javascriptrails ajax 控制器不会调用 javascript
【发布时间】:2013-06-24 10:50:11
【问题描述】:

我正在尝试为视频创建新评论

show.html.erb 在视频视图中

   <h5>New Comment</h5>

    <%= render 'comments/form' %>

  <div class = "show-comments">
    <% if @video.comments.size > 0 %>
      <h5>Comments</h5>

      <% for comment in @video.comments %>
        <div class = "well">
        <p><%= comment.content %></p>
        <small><%= comment.updated_at %></small>
        <% if comment.user_id == current_user.id %>
            <p align="right"><%= link_to 'delete', comment, method: :delete, data: { confirm: 'Are you sure?' } %></p>
        <% end %>
        </div>
      <% end %>
  <% end %>

  </div>   

_form.html.erb 在 cmets 视图中

  <%= form_for [@video, Comment.new], remote: true do |f| %>
      <p><%= f.text_area :content, :rows => 3 %></p>
      <p><%= f.submit %></p>
    <% end %>

在 cmets_controller.rb 中创建方法

 def create
    @video = Video.find(params[:video_id])
    @comment = Comment.new(params[:comment])

    current_user.comments << @comment
    @video.comments << @comment

    respond_to do |format|
      if @comment.save

        format.html { redirect_to @video, notice: 'Comment was successfully added.' }
        format.js

      end
    end
  end

创建.js.coffee.erb文件

new_comment = $('<p><%= @comment.content %></p>')
$(".show-comments").prepend(new_comment)

当我单击提交评论按钮时,它应该调用 javascript 并发布新评论,但它没有。

这是一条错误消息

Failed to load resource: the server responded with a status of 500 (Internal Server Error) http://localhost:3000/videos/51adc1ae25e218d5f6000001/comments
POST http://localhost:3000/videos/51adc1ae25e218d5f6000001/comments 500 (Internal Server Error) 

【问题讨论】:

  • ActionView::MissingTemplate - 缺少模板 cmets/create, application/create with {:locale=>[:en], :formats=>[:js, :html], :handlers=>[ :erb, :builder, :coffee]}.

标签: ajax ruby-on-rails-3 coffeescript


【解决方案1】:

尝试将您的文件重命名为 create.js.coffee

虽然没有写在文件扩展名中,但它仍然应该呈现 erb。

如果对你有帮助,请告诉我

【讨论】:

    猜你喜欢
    • 2018-05-28
    • 1970-01-01
    • 1970-01-01
    • 2018-08-29
    • 1970-01-01
    • 2016-09-26
    • 2018-10-22
    • 2016-03-26
    • 1970-01-01
    相关资源
    最近更新 更多