【发布时间】:2013-05-14 07:16:10
【问题描述】:
我正在使用 simple_form gem 并生成我指定 remote:true 选项的表单,如下所示:
<%= simple_form_for @webinar, validate: true, remote:true do |f| %>
所以,表单的输出 html 是以下片段:
<form accept-charset="UTF-8" action="/webinars" class="simple_form new_webinar" data-remote="true" data-validate="true" enctype="multipart/form-data" id="new_webinar" method="post" novalidate="novalidate"> ... </form>
正如我所检查的,当使用 remote:true 选项时,使用标准的 form_for 助手将 data-remote='true' 添加到表单中。从生成的 html 中可以看出,当我使用 simple_form gem 时,也有这样的属性。
所以,在我的控制器中,我有:
def create
@webinar = Webinar.new(params[:webinar])
respond_to do |format|
if @webinar.save
format.html { redirect_to @webinar, notice: 'Webinar was successfully created.' }
format.js
format.json { render json: @webinar, status: :created, location: @webinar }
else
format.html { render action: "new" }
format.json { render json: @webinar.errors, status: :unprocessable_entity }
end
end
end
但是,总是使用 format.html。我做错了什么?
编辑:
我使用了 logger.debug request.format 来检查请求的实际格式是什么,并且在日志文件中它是:
文本/html
所以,问题一定出在 simple_form 生成的表单中 - 当我们有“data-remote=true”时会有什么问题?
【问题讨论】:
-
您的
application.js中有//= require jquery和//= require jquery_ujs吗? `/app/assets/view/webinar' 下是否有create.js.erb文件? -
是的,我已经准备好了所有这些。我已经成功地对所有操作的部分进行了 ajax 请求,而无需创建和更新。我的 application.html.erb 文件中也有 。实际上,问题在于控制器的行为格式是 html 而不是 js。
-
您可以尝试将
:html => { :data => { :type => :json } }作为选项添加到 simple_form_for... -
@mccannf 也没有帮助。有什么建议我如何检查是否将什么类型的格式发送到我的控制器?
-
:(您可以在控制器中尝试使用
logger.debug request.format和logger.debug request.headers["Content-Type"]。
标签: ruby ajax ruby-on-rails-3 controller simple-form