Rails 3 form_for :remote => true 不通过 AJAX 提交
我正在尝试通过 AJAX 提交表单。我认为这应该是一件简单的事情,但它坚持以常规表单形式发布...
更新
此表单存在于另一个表单中,该表单不是 AJAX 化的或相关的?这会引起问题吗?
我得到的错误
LinkWidgetsController#create 中的参数错误
LinkWidgetsController 中没有默认布局 [/Users/victorstan/Sites/portrait/app/views, /Users/victorstan/.rvm/gems/ruby-1.9.2-p290@portrait/gems/devise-1.5.1/app/views]
相关代码:
路由
resources :profiles do
resource :link_widgets #this handles the form requests for create
member do
get 'new_link_widget' #this handles the form requests for new
end
resources :pages
end
控制器
class LinkWidgetsController < ActionController::Base
before_filter :authenticate_user!
load_and_authorize_resource
def create
@link_widget = LinkWidget.new(params[:link_widget])
respond_to do |format|
if @link_widget.save
format.js { render :js => @link_widget, :status => :created, :layout => !request.xhr? }
else
logger.debug @link_widget.errors.inspect
format.js { render :js => @link_widget.errors, :status => :unprocessable_entity }
end
end
end
end
部分
%hr
= form_for [@profile, @link_widget], :remote => true, :format => :js do |f|
.field
= f.label :provider
= f.select(:provider, options_for_select(f.object.providers, f.object.provider))
.field
= f.label :link
= f.text_field :link, size: 37, class: 'input-width'
.field
= f.label :title
= f.text_field :title, size: 37, class: 'input-width'
%div
%small.form-help-text If you set an optional title, it will show up beside the icon as a link
.field
= f.label :order
= f.text_field :order, size: 2
- unless f.object.new_record?
.field
= f.label :delete
= f.check_box :_destroy
= f.hidden_field :profile_id
.actions
= f.submit 'Save', class: 'right'
.clear
I am trying to submit a form via AJAX. I figured this should be a simple thing, but it insists on posting as a regular form...
update
This form exists within another form, which is not AJAXified, or related? Would that cause a problem?
The error I get
ArgumentError in LinkWidgetsController#create
There was no default layout for LinkWidgetsController in
[/Users/victorstan/Sites/portrait/app/views,
/Users/victorstan/.rvm/gems/ruby-1.9.2-p290@portrait/gems/devise-1.5.1/app/views]
Relevant code:
routes
resources :profiles do
resource :link_widgets #this handles the form requests for create
member do
get 'new_link_widget' #this handles the form requests for new
end
resources :pages
end
controller
class LinkWidgetsController < ActionController::Base
before_filter :authenticate_user!
load_and_authorize_resource
def create
@link_widget = LinkWidget.new(params[:link_widget])
respond_to do |format|
if @link_widget.save
format.js { render :js => @link_widget, :status => :created, :layout => !request.xhr? }
else
logger.debug @link_widget.errors.inspect
format.js { render :js => @link_widget.errors, :status => :unprocessable_entity }
end
end
end
end
partial
%hr
= form_for [@profile, @link_widget], :remote => true, :format => :js do |f|
.field
= f.label :provider
= f.select(:provider, options_for_select(f.object.providers, f.object.provider))
.field
= f.label :link
= f.text_field :link, size: 37, class: 'input-width'
.field
= f.label :title
= f.text_field :title, size: 37, class: 'input-width'
%div
%small.form-help-text If you set an optional title, it will show up beside the icon as a link
.field
= f.label :order
= f.text_field :order, size: 2
- unless f.object.new_record?
.field
= f.label :delete
= f.check_box :_destroy
= f.hidden_field :profile_id
.actions
= f.submit 'Save', class: 'right'
.clear
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我理解您的意思
此表单存在于另一个表单中
,则您已嵌套表单标签(如下所示):HTML 规范不允许嵌套表单标签。您应该将内部表单移至文档中的其他位置。如果这是不可能的,您可以尝试在单个表单中使用两个提交按钮,然后根据按下的按钮采取适当的操作(您可以在服务器上执行此操作)。
If I understand what you mean by
This form exists within another form
, you have nested form tags (something like the following):The HTML specification does not allow nesting of form tags. You should move the inner form to another place in your document. If that's not possible, you could try to use two submit buttons inside a single form, and then take appropriate action based on which button was pressed (you can do that on the server).