在 Rails 3 中使用 jQuery 渲染更多部分
我有一个包含许多图像的模型(假设它是一个用户)。我在控制器中构建了 3 个图像记录,并在视图中为每个记录渲染部分图像。但是如果人们想要添加 3 张以上的图片怎么办?我想创建一个“添加另一个图像”链接来构建另一个图像记录并呈现另一个部分。
这在 Rails 2.3 中非常简单,使用 add_object_link ,但我无法用 Rails 3 弄清楚它。我只能找到创建注释并从 create.js.erb 渲染它们的示例,这并不是我真正想要的寻找。
# users_controller.rb
def new
@user = User.new
3.times { @user.images.build }
end
# new.html.erb:
<%= f.fields_for :images do |builder| %>
<%= render 'image_form', :f => builder %>
<% end %>
是否有一种优雅的方法来创建一个链接来构建对象并呈现部分内容?
I have a model (let's say it's a User) that has many Images. I build 3 image records in the controller and render partials for each one in the view. But what if people want to add more than 3 pictures? I'd like to make an "Add another image" link that builds another image record and renders another partial.
This was pretty straightforward in Rails 2.3, with add_object_link, but I can't figure it out with Rails 3. I can only find examples of creating comments and rendering them from create.js.erb, which isn't really what I'm looking for.
# users_controller.rb
def new
@user = User.new
3.times { @user.images.build }
end
# new.html.erb:
<%= f.fields_for :images do |builder| %>
<%= render 'image_form', :f => builder %>
<% end %>
Is there an elegant way to create a link that builds an object and renders a partial?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看一下 Ryan Bates 编写的nested_form gem。
它利用 javascript 的强大功能完全满足您的需求。
Take a look at nested_form gem written by Ryan Bates.
It does exactly what you want with the power of javascript.