将 ajax 添加到 Rails 3 form_for
我正在学习编程,并在我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不确定这是否是您的创建操作的完整代码片段,但看起来您正在尝试对不存在的实例变量调用渲染...
@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 thecreate
method in theObjectController
...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 theProfile
class has a has_many relationship with yourObject
class, so if that was the right code, then you should typerender @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?