将 ajax 添加到 Rails 3 form_for

发布于 2024-12-10 14:21:34 字数 1814 浏览 1 评论 0原文

我正在学习编程,并在我的 Rails 3 应用程序中运行了一个表单。现在我尝试将 ajax 添加到表单中,以便页面在提交后不会重新加载。

我已经遵循了大量的教程,但似乎不太清楚如何将它们组合在一起。该表单通过以下模型将新的对象添加到配置文件

class Profile < ActiveRecord::Base
  has_many :objects
end

class Object < ActiveRecord::Base
  belongs_to :profile
end

我的表单位于views/profiles/_object_form.html.erb

<%= form_for(@object, :remote => true) do |f| %>
<% end %>

表单及其创建的对象在我的 views/profiles/_about.html.erb 中呈现:

<div id="newObjects">
  <%= render :partial => 'object_form' %>
</div>
<div id="objectList">
  <%= render :partial => 'object', :collection => @profile.objects, :locals => {:object_count => @profile.objects.length) %>
</div>

在我的 objects_controller.rb 中我有以下创建操作:

def create
  @object = Object.new(params[:object].merge(:author_id => current_user.id))
  respond_to do |format|
    if @object.save!
      format.html {redirect_to profile_path(@object.profile) }
      format.js { render }
    else
      format.html { redirect_to @profile, :alert => 'Unable to add object' }
    end
  end
end

views/objects/create.js.erb:

$('#objectList').append("<%= escape_javascript(render @profile.object)) %>");

因此,我有一个表单调用我想要添加的另一个控制器中的操作阿贾克斯。目前发生的情况是我需要重新加载配置文件以显示新创建的对象。我做错了什么?

澄清:除了 ObjectsController 中的创建操作之外,我仅在其他地方引用 @object 一次。这是在 ProfilesController 的显示操作中:

def show
  @profile = Profile.find(params[:id])
  @superlative = @profile.superlatives.new`
end

I'm learning to program and got a form running in my Rails 3 app. Now I'm attempting to add ajax to the form so the page doesn't reload after submitting.

I've followed the numerous tutorials but can't quite seem to figure out how to bring it together. The form adds new Objects to the Profile through the following model:

class Profile < ActiveRecord::Base
  has_many :objects
end

class Object < ActiveRecord::Base
  belongs_to :profile
end

My form in views/profiles/_object_form.html.erb:

<%= form_for(@object, :remote => true) do |f| %>
<% end %>

Where the form and its created objects are rendered in my views/profiles/_about.html.erb:

<div id="newObjects">
  <%= render :partial => 'object_form' %>
</div>
<div id="objectList">
  <%= render :partial => 'object', :collection => @profile.objects, :locals => {:object_count => @profile.objects.length) %>
</div>

In my objects_controller.rb I have the following create action:

def create
  @object = Object.new(params[:object].merge(:author_id => current_user.id))
  respond_to do |format|
    if @object.save!
      format.html {redirect_to profile_path(@object.profile) }
      format.js { render }
    else
      format.html { redirect_to @profile, :alert => 'Unable to add object' }
    end
  end
end

In views/objects/create.js.erb:

$('#objectList').append("<%= escape_javascript(render @profile.object)) %>");

So I have a form calling an action in another controller to which I want to add ajax. What happens at the moment is that I need to reload the profile to show the newly created object. What am I doing wrong?

CLARIFICATION: Other than the create action in the ObjectsController, I only reference @object once elsewhere. That's in the ProfilesController's show action:

def show
  @profile = Profile.find(params[:id])
  @superlative = @profile.superlatives.new`
end

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

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

发布评论

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

评论(1

眼前雾蒙蒙 2024-12-17 14:21:34

不确定这是否是您的创建操作的完整代码片段,但看起来您正在尝试对不存在的实例变量调用渲染... @profile 从未在 < ObjectController 中的 code>create 方法...

也许您想输入 $('#objectList').append("<%= escape_javascript(render @object) ) %>");

还注意到,在你的您正在调用 render @profile.object 的现有代码,但 Profile 类与您的 Object 类具有 has_many 关系,因此如果这是正确的代码,那么您应该输入 render @profile.objects(复数,而不是单数)。

但我认为您可能需要我上面提到的代码,因为您要附加到对象列表中,而不是再次渲染列表?

Not sure if this is a full code snippet for your create action, but looks like you are trying to call render on an instance variable that doesn't exist... @profile is never set in the create method in the ObjectController...

Perhaps you meant to type $('#objectList').append("<%= escape_javascript(render @object)) %>");

Also noticed that in your existing code you're making a call to render @profile.object, but the Profile class has a has_many relationship with your Object class, so if that was the right code, then you should type render @profile.objects (plural, not singular).

But I would think you would likely want the code I mentioned above, since you are appending onto the list of objects, not rendering the list again?

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