使用 jQuery 了解 Rails 变量持久性

发布于 2024-11-06 10:16:56 字数 2162 浏览 0 评论 0原文

当我通过支架控制器创建一个新对象时,如果我将一些 jquery 脚本操作到我的表单中,并且这些脚本触发我的控制器中的函数,则该函数不知道我刚刚创建的对象。为什么?

让我解释一下:

在我的控制器中,我首先创建我的对象:

  def new
    @projectmilestone = Projectmilestone.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @projectmilestone }
    end
  end

在我看来,我有一个带有一个嵌套部分的表单:

<%= semantic_form_for [:projects,@projectmilestone]  do |form| %>

    <%= form.semantic_errors :state %>
    <%= form.inputs do %>
      <%= form.input :department_id, :as => :select, :collection => Department.all%>
      <div id="stakeholders">
          <%=render :partial => "stakeholders", :locals => {:users => @users }%>
      </div>

    <%  end %>

    <%= form.submit "Submit" %>

<% end %>

利益相关者部分是这样的:

<%= semantic_form_for [:projects,@projectmilestone], :remote => true  do |form| %>
    <%= form.inputs :partial do %>
      <%= form.input :user_id , :as => :select, :collection => users %>
    <% end %>
<% end %>

当我更改department_id值时,我的应用程序中会触发一个javascript函数。 js 文件:

$('#projectmilestone_department_id').live('change', function() {
    // make a POST call and replace the content
    var department = $('select#projectmilestone_department_id :selected').val();
    if(department == "") department="0";
    var milestone = $('#form_id').html();
    jQuery.get('/projects/projectmilestones/update_user_select/' + department, function(data){
        $("#stakeholders").html(data);
    })
    return false;
  });

此函数调用我的控制器中的 update_user_select 函数:

 def update_user_select
    users=""
    users = User.where(:department_id=>params[:department_id]) unless params[:department_id].blank?
    render :partial => "stakeholders", :locals => {:users => users }
  end

然后用户部分应该重新加载到我的表单中,但事实并非如此,因为 @projectmilestone 为零。

为什么当我使用此 jquery 脚本时 @projectmilestone 不持久,而当我转到脚手架的创建函数时 @projectmilestone 不持久?

非常感谢!

When I create a new object via a scaffolded controller, if I actionate some jquery script into my form and that these scripts trigger a function in my controller, this function doesn't know about the object I just created. Why?

Let me explain:

In my controller, I first create my object:

  def new
    @projectmilestone = Projectmilestone.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @projectmilestone }
    end
  end

In my view, I have a form with one nested partial:

<%= semantic_form_for [:projects,@projectmilestone]  do |form| %>

    <%= form.semantic_errors :state %>
    <%= form.inputs do %>
      <%= form.input :department_id, :as => :select, :collection => Department.all%>
      <div id="stakeholders">
          <%=render :partial => "stakeholders", :locals => {:users => @users }%>
      </div>

    <%  end %>

    <%= form.submit "Submit" %>

<% end %>

The stakeholders partial is like this:

<%= semantic_form_for [:projects,@projectmilestone], :remote => true  do |form| %>
    <%= form.inputs :partial do %>
      <%= form.input :user_id , :as => :select, :collection => users %>
    <% end %>
<% end %>

When I change the department_id value, a javascript function is triggered in my application.js file:

$('#projectmilestone_department_id').live('change', function() {
    // make a POST call and replace the content
    var department = $('select#projectmilestone_department_id :selected').val();
    if(department == "") department="0";
    var milestone = $('#form_id').html();
    jQuery.get('/projects/projectmilestones/update_user_select/' + department, function(data){
        $("#stakeholders").html(data);
    })
    return false;
  });

This function calls the function update_user_select in my controller:

 def update_user_select
    users=""
    users = User.where(:department_id=>params[:department_id]) unless params[:department_id].blank?
    render :partial => "stakeholders", :locals => {:users => users }
  end

The users partial should then reload in my form but it doesn't because @projectmilestone is nil.

Why is @projectmilestone not persisted when I use this jquery script and persisted when there i go to the create function of my scaffold?

Many thanks!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

梦里人 2024-11-13 10:16:56

原因是当 Rails 渲染页面时,您在控制器中设置的实例变量在视图和局部视图之间共享。
它是堆栈的一部分,服务器端并发送到浏览器中的客户端。

但是,当您使用 Ajax 时,您是在客户端工作,服务器不再知道它之前发送的内容,除非您通过发送正在处理的对象的 id 来回忆它。

The reason is when Rails renders your page, the instance variable you set in the controller is shared between the view and the partials.
It's part of the stack, server side and sent to the client, in your browser.

But when you use Ajax, you're working from the client and the server doesn't know anymore about what it sent before unless you recall it by sending the id of the object you're working on.

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