weixin_33671935 2015-05-22 08:17 采纳率: 0%
浏览 18

如何在Rails 4上使用Ajax?

I am trying to create a comment area in rails 4 using ajax. My target is to create comment and its output in same page without page loading. I have added some code. When i click on "add comment" button the data entered to DB, but does not reflect in same page.Please share with me if any one have any idea, thank u.

My codes are below:

In comment controller:

def create
    @article = Article.find(params[:article_id])
    @comment = @article.comments.create
    @comment.body = params[:comment]["body"]

    respond_to do |format|
        @comment.save
        format.html { redirect_to @article }
        format.js
    end
end

In show.html.erb under views/articles

<div class="col-xs-12 col-sm-12">
  <h2 class="text-center"><%= @article.title.html_safe %></h2>
    <p><%= @article.body.html_safe %></p>
      <h2>Comments</h2>
        <div id="comments">
             <!--  <p><%= render :partial => @article.comments %></p> -->
             <%= render :partial => 'comments/comment', :collection => @article.comments %>
        </div>
        <%= form_for([@article, Comment.new], :remote => true) do |f| %>
              <p>
                <%= f.label :body, "New comment" %><br/>
                <%= f.text_area :body, type:"text", tabindex: "6", class: "form-control",  placeholder: 'your comment', rows: "4", required: true  %>
              </p>
              <p><%= f.submit "Add comment" %></p>
        <% end %>
</div>

_comment.html.erb under view/comments

<%= div_for comment do %>
    <p>
        <b>
           Posted <%= time_ago_in_words(comment.created_at) %> ago
        </b>
        <br/>
        <%= comment.body %>
           <%= link_to 'Delete', article_comment_path(comment.article_id, comment.id), method: :Delete, :class => 'btn btn-sm btn-warning', tabindex: "3" %>
           <%= link_to 'Edit', article_path(comment.article_id, comment.id), :class => 'btn btn-sm btn-warning', tabindex: "3" %>                         
    </p>
<% end %>    

create.js.erb under views/comments

page.insert_html :bottom, :comments, :partial => 'comment', :object => @comment
page[:new_comment].reset

config/application.rb

config.action_view.JavaScript_expansions[:defaults] = %w(jquery rails application)
  • 写回答

2条回答 默认 最新

  • weixin_33699914 2015-05-22 09:49
    关注

    the proper way to handle this case in rails is using a remote form, this way you can interactively insert and remove objects from db. Here are two explained blog posts about remote forms in rails detailed guide about remote forms and how to partials ajax rails. This way you can submit the form without refresh an then have an handler to generate the created element after the callback from the controller. Hope this fits your question.

    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵