开发者

Rendering more partials with jQuery in Rails 3

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
  @u开发者_JAVA技巧ser = 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?


Take a look at nested_form gem written by Ryan Bates. It does exactly what you want with the power of javascript.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜