Rails 3 form_for :remote => true 不通过 AJAX 提交

发布于 2024-12-18 05:59:52 字数 2016 浏览 1 评论 0原文

我正在尝试通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷情 2024-12-25 05:59:52

如果我理解您的意思 此表单存在于另一个表单中,则您已嵌套表单标签(如下所示):

<form> <!-- this is a standard form -->
    ...
    <form> <!-- this is the remote/AJAXified form -->
        ...
    </form>
    ...
</form>

HTML 规范不允许嵌套表单标签。您应该将内部表单移至文档中的其他位置。如果这是不可能的,您可以尝试在单个表单中使用两个提交按钮,然后根据按下的按钮采取适当的操作(您可以在服务器上执行此操作)。

If I understand what you mean by This form exists within another form, you have nested form tags (something like the following):

<form> <!-- this is a standard form -->
    ...
    <form> <!-- this is the remote/AJAXified form -->
        ...
    </form>
    ...
</form>

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文