[英]Rails Ajax Jquery
I'm working on an dynamic edit of an article using rails. 我正在使用Rails对文章进行动态编辑。 On each articles can be added paragraphs.
在每个文章上都可以添加段落。 I wanted to use Ajax with Jquery following the 136-jquery-ajax-revised tutorial from railscast.
我想按照Railscast的136-jquery-ajax修订的教程使用Ajax和Jquery。 I created the template for the form and the new.js.erb response but I keep geting same error:
我为表单和new.js.erb响应创建了模板,但始终收到相同的错误:
ParagraphsController#new is missing a template for this request format and variant. ParagraphsController#new缺少此请求格式和变体的模板。 request.formats: ["text/html"] request.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 查看/段落/new.js.erb
$('div#article-control-panel').hide("fast", function () {
$('div#article-control-panel').after('<%= j render 'form' %>')
})
controllers/paragraphs_controller 控制器/ parameters_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... 编辑奇怪的事情是,如果我尝试在诸如article_path之类的地方更改orm的网址,它将起作用...
Your controller is missing a respond_to
. 您的控制器缺少
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. 这
respond_to
允许导轨调用相关.js.erb
文件,在这种情况下您new.js.erb
文件。
Just keep in mind that when you want to perform an ajax call, you require these few elements: 请记住,当您要执行ajax调用时,需要以下几个元素:
A respond_to { |format| format.js }
一个
respond_to { |format| format.js }
respond_to { |format| format.js }
in your controller action 控制器动作中的
respond_to { |format| format.js }
Your js.erb
file needs to be the same name as your controller action ( foo.js.erb
) 您的
js.erb
文件需要与控制器操作( foo.js.erb
) foo.js.erb
A partial to render through your js.erb
file, typically named the same as your js file. 通过
js.erb
文件呈现的js.erb
文件,通常与js文件命名相同。 ( _foo.js.erb
) (
_foo.js.erb
)
If it is a link, you need remote: true
. 如果是链接,则需要
remote: true
。 If it is a form and you're using rails 5, you could use form_with
or simply include remote: true
too. 如果它是表单,并且您正在使用rails 5,则可以使用
form_with
或仅包含remote: true
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.