1

I am trying to figure out how to register certain values to their respective table using the form, check the order form to better understand which values need to be registering to which table.

I have also displayed the table entries, models and controller related to this question. If someone can guide me to where I can obtain further understanding on coding the associations in forms that would be great.

order form

<%= simple_form_for(@order) do |f| %>
    <%= f.association :items, collection: Item.all, label_method: :name, value_method: :id %>
    <%= [need to display the price of the item selected] %>
    <%= f.input :quantity ???? [need to register in the order_items table] %>
    <%= [register sub total to orders table] %>
    <%= f.submit %>
<% end %>

tables

  create_table "order_items", force: true do |t|
    t.integer  "item_id"
    t.integer  "order_id"
    t.integer  "quantity"
  end

  create_table "orders", force: true do |t|
    t.integer  "user_id"
    t.integer  "client_id"
    t.boolean  "status"
    t.decimal  "sub_total"
  end

  create_table "items", force: true do |t|
    t.string   "name"
    t.decimal  "price"
    t.integer  "stock"
  end

models

class Order < ActiveRecord::Base
        ...
    has_many :order_items
    has_many :items, :through => :order_items
end

class Item < ActiveRecord::Base
        ...
    has_many :order_items
    has_many :orders, :through => :order_items
end

class OrderItem < ActiveRecord::Base
    belongs_to :item
    belongs_to :order
end

orders controller

def create
    @order = Order.new(order_params)
    @order.user_id = current_user.id
    @order.status = TRUE
end

def order_params
    params.require(:order).permit(:code, :client_id, :user_id, :memo, :status, item_ids: [])
end
Baldrick
  • 23,882
  • 6
  • 74
  • 79
lightbots
  • 485
  • 3
  • 13
  • Is there more code in your Orders controller? – Beartech May 30 '14 at 16:37
  • It appears your form would allow them to pick one item and a quantity for that item. Your controller should then update the order table and the order_items table, correct? There should be some sort of `.build` going on in your controller I think. – Beartech May 30 '14 at 16:43
  • check this out: http://stackoverflow.com/questions/12832546/simple-form-association-with-has-many-through-extra-field?rq=1 – Beartech May 30 '14 at 16:44
  • I have more code in the controller but nothing with build, what does the .build do? interesting link you posted. – lightbots May 30 '14 at 21:15
  • The idea with things like `accepts_nested_attributes_for` nested route resources, nested forms, `.build`, and association methods, is to make life easier for doing things like you propose here. You should be able to use a nested form that has `f.fields_for` to accept associated information that gets put in other tables automatically. I'm rusty enough at the moment that I can't just whip up an answer for you. I highly recommend you watch these rails-casts: http://railscasts.com/episodes/196-nested-model-form-part-1 (and part 2) – Beartech May 31 '14 at 00:18
  • also: http://railscasts.com/episodes/139-nested-resources and: http://railscasts.com/episodes/73-complex-forms-part-1 , http://railscasts.com/episodes/74-complex-forms-part-2 , http://railscasts.com/episodes/75-complex-forms-part-3 – Beartech May 31 '14 at 00:21
  • And finally the documentation: http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#method-i-has_many – Beartech May 31 '14 at 00:26
  • Thank you @Beartech, I will start watching the tutorials before continuing. – lightbots Jun 02 '14 at 15:05
  • @Beartech most of the tutorials are out of date and point to episode 196, I am not sure if watching the out of date ones will help, I think the cocoon gem can replace episode 196 in a way – lightbots Jun 02 '14 at 15:27
  • 1
    I think you really need to dig into ActiveRecord to do what you want. Also if you want the page to update to show the price of an item once it is selected from the drop down list you would probably want to use JavaScript. – Beartech Jun 03 '14 at 05:03
  • I found the following answer to be useful. http://stackoverflow.com/questions/7844568/ror-3-creating-an-invoice-app-how-do-i-create-the-form-for-a-habtm-invoice-pro – lightbots Jun 19 '14 at 12:31
  • 1
    You may find the following link to be useful. http://stackoverflow.com/questions/7844568/ror-3-creating-an-invoice-app-how-do-i-create-the-form-for-a-habtm-invoice-pro – lightbots Jun 19 '14 at 12:37

0 Answers0