导轨 - 更新DIV W / Ajax和:远程=>真正 [英] Rails - updating div w/ Ajax and :remote => true

查看:96
本文介绍了导轨 - 更新DIV W / Ajax和:远程=>真正的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我跟着这个 Railscast 上提交通过Ajax的表单和更新一个div无需重新加载页面,但我无法与它的一个部分。

瑞安 $(#产品)HTML(<%= escape_javascript(渲染(@products))%>); index.js.erb的文件更新 #products DIV当表单提交。我努力理解像一个实例变量 @products 是否与我的情况,或者如果我可以简单地用它替换的URL。

我试图做同样的事情,瑞安在这个截屏,但不是显示搜索结果我只是想显示更新后的值。

在show.html.erb我有:

 <%@ project.project_todos.order(created_at DESC)的地方。(:状态=>假)。每个做|待办事项|%GT;

<%=的form_for待办事项,:远程=>如此,数据代替=> #dueon做| F | %>
   <%= f.text_field:由于%>
   <%= f.submit:类=> 初级,:价值=> 添加%>
<%结束%GT;

< D​​IV ID =dueon>
   <%=渲染交货期,:待办事项=>待办事项%>
< / DIV>

<%结束%GT;
 

局部 _duedate.html.erb 有句台词在里面:<%= todo.due%>

因此​​,在我index.js.erb的我现在有这样的: $(#dueon)HTML(<%= escape_javascript(渲染(_交货期))%>中。 ); ,但它显然不能正常工作。我必须代替这里使用一个变量 _duedate ?如果是的话,我将如何设置此控制器?我的意思是什么呢变量已经重新present?

另外,对于它的价值,部分是正确的渲染和显示 todo.due 价值......它只是没有更新,当我提交表单。

ProjectsController:

 高清节目
    @project = Project.find(PARAMS [:ID])

    #显示窗体创建一个新的待办事项
    @project_todo = ProjectTodo.new

    respond_to代码做|格式|
      的format.html#show.html.erb
      format.json {渲染:JSON => @项目 }
    结束
  结束
 

解决方案

试试这个

在你的控制器动作,(说sample_action)

 高清sample_action
   @todos = #your code

   respond_to代码做|格式|
      format.js
   结束
结束
 

和你有一个 sample_action.js.erb

  $(#dueon)HTML(<%=原escape_javascript(渲染(:部分=>交货期))%>中)
 

然后在里面的部分,你可以访问新的 @todos 实例变量

心连心

I've followed this Railscast on submitting a form via Ajax and updating a div without reloading the page, but I'm having trouble with one portion of it.

Ryan has $("#products").html("<%= escape_javascript(render(@products)) %>"); in an index.js.erb file to update the #products div when the form is submitted. I'm struggling to understand whether an instance variable like @products is relevant for my situation, or if I can simply substitute it for a URL.

I'm trying to do the same thing as Ryan in this screencast, but instead of display search results I just want to display the updated value.

In show.html.erb I have:

<% @project.project_todos.order("created_at DESC").where(:status => false).each do |todo|%>

<%= form_for todo, :remote => true, :"data-replace" => "#dueon" do |f| %>
   <%= f.text_field :due %>
   <%= f.submit :class => "primary", :value => "Add" %>
<% end %>

<div id="dueon">
   <%= render "duedate", :todo => todo %>
</div>

<% end %>

The partial _duedate.html.erb has one line in it: <%= todo.due %>

So in my index.js.erb I currently have this: $("#dueon").html("<%= escape_javascript(render("_duedate")) %>"); but it's clearly not working. Do I have to use a variable here in place of the _duedate? And if so, how would I set this up in the controller? I mean what does the variable have represent?

Also, for what it's worth, the partial is rendering correctly and displaying the todo.due value...it's just not updating when I submit the form.

ProjectsController:

def show
    @project = Project.find(params[:id])

    # Display the form to create a new todo
    @project_todo = ProjectTodo.new

    respond_to do |format|
      format.html # show.html.erb
      format.json { render :json => @project }
    end
  end

解决方案

Try this

in you controller action , (say sample_action)

def sample_action
   @todos = #your code

   respond_to do |format|
      format.js
   end
end

and you have a sample_action.js.erb

$("#dueon").html("<%= raw escape_javascript(render(:partial => 'duedate')) %>")

then inside the partial, you have access to the new @todos instance variable

HTH

这篇关于导轨 - 更新DIV W / Ajax和:远程=&GT;真正的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆