怎么说在Rails3中调用这个Ajax?

时间:2022-10-08 23:35:08

I am struggling with this Ajax call from Rails 2:

我正在努力从Rails 2调用这个Ajax:

   def add_task_link(name)
      link_to_function name do |page|
         page.insert_html :bottom, :tasks, :partial => 'task', :object => Task.new
      end
   end

How can I express this with Rails 3?

我如何用Rails 3表达这个?

Thanks,

谢谢,

2 个解决方案

#1


1  

I think you might want link_to :remote => true instead. The link_to_function is meant to accept a string of javascript code that it puts in the onclick attribute. So instead of your helper method, use something like

我想你可能想要link_to:remote => true。 link_to_function旨在接受它放在onclick属性中的一串javascript代码。所以不要使用辅助方法,而是使用类似的东西

<%= link_to 'My Link', new_task_path, :remote => true %>

Then in your new action, you need to add a .js respond to block.

然后在您的新操作中,您需要添加.js响应块。

def new
  @task = Task.new
  respond_to do |format|
    format.html
    format.js { render :layout => false } # add this line
  end
end

Then create a file under app/views/tasks called new.js.erb, and add:

然后在名为new.js.erb的app / views / tasks下创建一个文件,并添加:

$('body').append('<%= escape_javascript(render :partial => "form", :locals => { :task => @task }) %>');

Provided the partial form is setup (which you can use anyways for your new.html.erb and edit.html.erb files) that should work just fine. Took some examples from this example repository: https://github.com/nu7hatch/rails3-ujs-example/blob/master/ I have to admit, I don't do a lot of link_to remote coding examples in Rails. Most ajax calls I do are using JSON responses. So I write the js code under the asset pipeline.

如果设置了部分表单(您可以将其用于new.html.erb和edit.html.erb文件),这应该可以正常工作。从这个示例存储库中获取一些示例:https://github.com/nu7hatch/rails3-ujs-example/blob/master/我不得不承认,我在Rails中没有做很多link_to远程编码示例。我做的大多数ajax调用都使用JSON响应。所以我在资产管道下编写了js代码。

#2


0  

There are some hints in this project in Github: https://github.com/ryanb/complex-form-examples

这个项目在Github中有一些提示:https://github.com/ryanb/complex-form-examples

   def link_to_add_fields(name, f, association)
     new_object = f.object.class.reflect_on_association(association).klass.new
     fields = f.fields_for(association, new_object, :child_index => "new_#{association}") do |builder|
         render(association.to_s.singularize + "_fields", :f => builder)
      end
      link_to_function(name, "add_fields(this, \"#{association}\", \"#{escape_javascript(fields)}\")")
    end

Additionally, it is important to use accepts_nested_attributes_for as shown in

此外,使用accepts_nested_attributes_for非常重要,如图所示

https://github.com/ryanb/complex-form-examples/blob/master/app/models/survey.rb

https://github.com/ryanb/complex-form-examples/blob/master/app/models/survey.rb

#1


1  

I think you might want link_to :remote => true instead. The link_to_function is meant to accept a string of javascript code that it puts in the onclick attribute. So instead of your helper method, use something like

我想你可能想要link_to:remote => true。 link_to_function旨在接受它放在onclick属性中的一串javascript代码。所以不要使用辅助方法,而是使用类似的东西

<%= link_to 'My Link', new_task_path, :remote => true %>

Then in your new action, you need to add a .js respond to block.

然后在您的新操作中,您需要添加.js响应块。

def new
  @task = Task.new
  respond_to do |format|
    format.html
    format.js { render :layout => false } # add this line
  end
end

Then create a file under app/views/tasks called new.js.erb, and add:

然后在名为new.js.erb的app / views / tasks下创建一个文件,并添加:

$('body').append('<%= escape_javascript(render :partial => "form", :locals => { :task => @task }) %>');

Provided the partial form is setup (which you can use anyways for your new.html.erb and edit.html.erb files) that should work just fine. Took some examples from this example repository: https://github.com/nu7hatch/rails3-ujs-example/blob/master/ I have to admit, I don't do a lot of link_to remote coding examples in Rails. Most ajax calls I do are using JSON responses. So I write the js code under the asset pipeline.

如果设置了部分表单(您可以将其用于new.html.erb和edit.html.erb文件),这应该可以正常工作。从这个示例存储库中获取一些示例:https://github.com/nu7hatch/rails3-ujs-example/blob/master/我不得不承认,我在Rails中没有做很多link_to远程编码示例。我做的大多数ajax调用都使用JSON响应。所以我在资产管道下编写了js代码。

#2


0  

There are some hints in this project in Github: https://github.com/ryanb/complex-form-examples

这个项目在Github中有一些提示:https://github.com/ryanb/complex-form-examples

   def link_to_add_fields(name, f, association)
     new_object = f.object.class.reflect_on_association(association).klass.new
     fields = f.fields_for(association, new_object, :child_index => "new_#{association}") do |builder|
         render(association.to_s.singularize + "_fields", :f => builder)
      end
      link_to_function(name, "add_fields(this, \"#{association}\", \"#{escape_javascript(fields)}\")")
    end

Additionally, it is important to use accepts_nested_attributes_for as shown in

此外,使用accepts_nested_attributes_for非常重要,如图所示

https://github.com/ryanb/complex-form-examples/blob/master/app/models/survey.rb

https://github.com/ryanb/complex-form-examples/blob/master/app/models/survey.rb