Guest User

Untitled

a guest
Apr 21st, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. ### Product model ###
  2. class Product < ActiveRecord::Base
  3. has_one :line_item, :foreign_key => 'item_id', :conditions => {:kind => 2}
  4. validates_presence_of :kind, :description
  5.  
  6. def price
  7. line_item.price
  8. end
  9.  
  10. def line_item_update_attributes=(line_item_attributes)
  11. line_item_attributes.each do |attributes|
  12. line_item.update_attributes(attributes.last)
  13. end
  14. end
  15.  
  16. end
  17.  
  18. ### View for product ###
  19.  
  20. <h2>Editing product</h2>
  21.  
  22. <%= error_messages_for :product %>
  23.  
  24. <% form_for(@product) do |f| %>
  25. <p>
  26. <b>Kind of Product</b><br />
  27. <%= f.text_field :kind %>
  28. </p>
  29.  
  30. <p>
  31. <b>Description</b><br />
  32. <%= f.text_field :description %>
  33. </p>
  34.  
  35. <p>
  36. <b>Price</b><br />
  37. <% fields_for "product[line_item_update_attributes][]", @product.line_item do |lif| %>
  38. <%= lif.text_field :price %>
  39. <% end %>
  40. </p>
  41.  
  42. <p>
  43. <%= f.submit "Update" %>
  44. </p>
  45. <% end %>
  46.  
  47. <%= link_to 'Show', @product %> |
  48. <%= link_to 'Back', products_path %>
Add Comment
Please, Sign In to add comment