0

Asking for best practice to list nested attributes in show action when you need to paste them in various places..

I have two models: Product & ProductDetail, ProductDetails belongs to Product, Product has_many ProductDetails. Within forms and nested renders it works like a charm, no issues (especially after RailsCast 196/197 by Ryan (thanks boet!)

Now, I need to issue view for my product. Basically it is just a product in commerce.

So in my controllers I have regular things like

@product = Product.find ... 
@product_details = @product.product_details.build

When in show action view

[email protected] 
[email protected]
[email protected] 
    -@product_details.each do |d|
=d.attribute1
=d.attribute2

... some other view details here.. 

    -@product_details.each do |d|
=d.attribute3
=d.attribute3

As you can see I put loops into view several time as I need show them in various places.

I wonder to know what is best practice (may be arrays make sense for here), please suggest.

I'am planning to extend page functionality by using radio buttons and jQuery events for changing (i.e. price) and values of certain field, which at the end changes price and order detail.

Would be great to get some good reading / example of rails + js combination specifically for nested models and its attributes.. the other thing is that I am planning to build number of nested models around main product and show them / update / edit etc based on client behavior. Is there any best practice on number of nested models per model ?

1 Answer 1

1

I'm sure you probably found this eventually - but just to touch up on this one ...

Yes, you want arrays passing to partials using the 'render' command to keep it 'DRY'. This allows you to reuse the fields in all sections you want with minimal hassle, per the Ruby Way...check the tutorial on partials/layout for views too(Link). I've never read the advanced pattern book - and things might have changed in the last year. I know a bunch of people randomly suggest using Cocoon with simpleForm, sometimes HAML comes up too to replace ruby's *.erb partial rendering.

You're missing the second part he did ... very helpful to me was the additional update did #403: Dynamic Form. Specifically he calls for rending by the type of checkbox which gives you a loading checkbox type ... it's dynamic partials loading...which is the answer on how to best repeat for each of these in a loop.

app / views / products / _form.html.erb

  <%= f.fields_for :properties, OpenStruct.new(@product.properties) do |builder| %>
    <% @product.product_type.fields.each do |field| %>
      <%= render "products/fields/#{field.field_type}", field: field, f: builder %>
    <% end %>
  <% end %>
Sign up to request clarification or add additional context in comments.

6 Comments

This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient reputation you will be able to comment on any post. - From Review
@RickSmith revised to point to a specific section in mind for future users to quickly snap too. Would appreciate a reverse in the down vote.
@ForceMagic ... updated to include details from the link - which the other guy admitted was the answer. Would appreciate a reverse in the down vote. Thanks.
@ForceMagic ah - ty then!
@RaptorMan I'm afraid I also was not the downvote :( I'll upvote you to try to make it even though...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.