weixin_33709590 2018-06-14 14:07 采纳率: 0%
浏览 62

Rails Ajax jQuery

I'm working on an dynamic edit of an article using rails. On each articles can be added paragraphs. I wanted to use Ajax with Jquery following the 136-jquery-ajax-revised tutorial from railscast. I created the template for the form and the new.js.erb response but I keep geting same error:

ParagraphsController#new is missing a template for this request format and variant. request.formats: ["text/html"] request.variant: []

This is the link that request the new form from view/article/show

<%= link_to 'New Paragraph', new_article_paragraph_path(@article), remote: true, class: 'uk-button uk-button-primary' %>

view/paragraph/_form

<div class="article-form-container">
  <%= form_with scope: :paragraph, url: article_paragraphs, remote: true do |f| %>
    <%= f.text_field :title, class: 'uk-input', placeholder: 'Title of paragraph (optional, can be a subtitle of the article)' %>
    <%= f.text_area :text, class: 'uk-textarea', placeholder: 'Content of paragraph' %>
    <%= f.hidden_field :position, :value => 3 %>
    <div class="submit-button">
    <%= f.submit class: 'uk-button uk-button-primary' %>
    </div>
  <% end %>
</div>

routes

resources :articles, only: %i[show update destroy create]

resources :articles do
  resources :paragraphs, only: %i[new create update destroy]
end

view/paragraphs/new.js.erb

$('div#article-control-panel').hide("fast", function () {
    $('div#article-control-panel').after('<%= j render 'form' %>')
})

controllers/paragraphs_controller

class ParagraphsController < ApplicationController
  def new
    @paragraph = Paragraph.new
  end

  def create
    @paragraph = Paragraph.new(paragraph_params)
    @article = Article.find(params[:article_id])

    if @article.user == current_user
      @paragraph.article = @article
      @paragraph.save
    end

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

  def paragraph_params
    params.require(:paragraph).permit(:title, :text, :position)
  end
end

Can't figure out what the problem is. The error happens in the article page, after I press the link button.

Edit Strange thing is that if i try to change the url of orm in something like article_path it will work...

  • 写回答

1条回答 默认 最新

  • weixin_33720186 2018-06-14 14:56
    关注

    Your controller is missing a respond_to. Your new function should look like this:

    def new
      @paragraph = Paragraph.new
      respond_to { |format| format.js }
    end
    

    This respond_to allows rails to call the relevant .js.erb file, in this instance your new.js.erb file.

    Just keep in mind that when you want to perform an ajax call, you require these few elements:

    • A respond_to { |format| format.js } in your controller action

    • Your js.erb file needs to be the same name as your controller action (foo.js.erb)

    • A partial to render through your js.erb file, typically named the same as your js file. (_foo.js.erb)

    • If it is a link, you need remote: true. If it is a form and you're using rails 5, you could use form_with or simply include remote: true too.

    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?