mysql - Rails: Issue with recieving nested forms with has many through join -
i seem having problem receiving products through join table, it's giving me strange error seems receiving no id order. can assume because order has not been created yet, creating order during step anyway, order doesn't have id yet. problem.
here error recieve:
activerecord::recordnotfound in orderscontroller#create couldn't find product id=1 order id= rails.root: /billingsystem application trace | framework trace | full trace app/controllers/orders_controller.rb:10:in `new' app/controllers/orders_controller.rb:10:in `create' request parameters: {"utf8"=>"✓", "authenticity_token"=>"je2wderoxe7pkwbhn60kafguxwaq8qdw4wbru51smfg=", "order"=>{"client_id"=>"1", "products_attributes"=>{"1368396234677"=>{"id"=>"1", "_destroy"=>"false"}}}, "commit"=>"create order"} show session dump show env dump response headers: none
new order view:
<% if current_user %> <div id="dashboard"> <div id="logo"></div> <table id="go_back_link_container"> <tr> <td> <div class="go_back_link"> <%= link_to "<- go back", "/orders/view" %> </div> </td> <td> <div id="user_display"> logged in <%= current_user.email %>. <%= link_to "log out", log_out_path %> </div> </td> </tr> </table> <%= form_for @order, method: :post |f| %> <% if @order.errors.any? %> <div class="error_messages"> <% message in @order.errors.full_messages %> * <%= message %> <br> <% end %> </div> <% end %> <p> <%= f.label 'select client' %><br /> <%= select :order, :client_id, client.all().collect { |c| [ (c.firstname + " " + c.surname), c.id ] } %> </p> <%= f.fields_for :products |pf| %> <% #render 'product_fields', f: builder %> <% end %> <%= link_to_add_fields "add product", f, :products %> <p class="button"><%= f.submit %></p> <% end %> <% flash.each |name, msg| %> <%= content_tag :div, "* " + msg, :id => "flash_#{name}" %><br /> <% end %> <div id="copyright-notice"><div id="copyright_border">copyright © conner mccabe, rights reserved.</div></div> </div> <% else %> <script type="text/javascript"> window.location="<%= root_url %>" </script> <% end %>
order model:
class order < activerecord::base has_many :orderedproducts has_many :products, through: :orderedproducts has_one :client attr_accessible :client_id, :order_total, :delivery_date, :products, :products_attributes accepts_nested_attributes_for :products, :allow_destroy => true before_save :generate_total def generate_total self.order_total = self.products.map(&:product_price).sum end end
orders controller:
class orderscontroller < applicationcontroller def view @orders = order.all end def new @order = order.new end def create @order = order.new(params[:order]) if @order.save redirect_to '/orders/view', :notice => "order created!" else render "new" end end end
product fields partial:
<fieldset> <%= f.select :id, product.all().collect {|p| [ p.product_name, p.id ] } %> <%= f.hidden_field :_destroy %> <%= link_to "remove", '#', class: "remove_fields" %> </fieldset>
products model:
class product < activerecord::base #this line makes these elements accessible outside of class. attr_accessible :product_name, :product_price, :product_quantity, :product_supplier has_many :orderedproducts has_many :orders, through: :orderedproducts #these attributes ensure data entered each element valid , present. validates_presence_of :product_name validates_presence_of :product_price validates_numericality_of :product_price validates_presence_of :product_quantity validates_numericality_of :product_quantity validates_presence_of :product_supplier end
application helper:
module applicationhelper def link_to_add_fields(name, f, association) new_object = f.object.send(association).klass.new id = new_object.object_id fields = f.fields_for(association, new_object, child_index: id) |builder| render(association.to_s.singularize + "_fields", f: builder) end link_to(name, '#', class: "add_fields", data: {id: id, fields: fields.gsub("\n", "")}) end end
ordered products model:
class orderedproduct < activerecord::base attr_accessible :order_id, :product_id, :quantity_ordered belongs_to :order belongs_to :product end
i have listed every possible file contain error, know it's bit excessive, it's , better include not @ all.
i followed railscast guide: http://railscasts.com/episodes/196-nested-model-form-revised am, edited suitable application.
thanks in advance.
we had similar issue on project, except relation singular. problem activerecord looking existing association; order.products.find(1). since order new record doesn't work.
you create own products_attributes= method , define correct behaviour. think use nested attributes join model (orderedproduct) instead of product.
class order accepts_nested_attributes_for :orderedproducts end
then adjust form fields appropriately. in new form
f.fields_for :products |pf|
becomes f.fields_for :orderedproducts |pf|
in fields partial
<%= f.select :id, product.all().collect {|p| [ p.product_name, p.id ] } %>
becomes <%= f.select :product_id, product.all().collect {|p| [ p.product_name, p.id ] } %>
Comments
Post a Comment