0

How do i make it so users who are filling out their form when adding new prices can select categories they already made or create a new one.

I have an Add_Prices controller,model and views. The Model holds the price name, date and price as of now.

  1. I don't know if the category should be a string or integer.

  2. I also don't know how to make the form so the user can select a scroll down list on the form of past categories they saved or in the scroll down list they select "New" and a text box pops up next to it and they can save it when they fill out everything.

  3. Everything(price,date,etc) including the category will go to the Data Controller/View so they can see everything there.

Please help a newbie!

Thank you!

LearningRoR
  • 26,582
  • 22
  • 85
  • 150

1 Answers1

1

Well, you would ideally name your controller (Link just so you can see naming conventions) the name of your resource so consider using just Price instead of Add_Prices. And as for adding categories, consider adding another resource for categories and use rails associations to associate the categories to other objects. Using this type of relationship is demonstrated over in this question here. The category should have a name that is text and it should also have an ID generated by default.

Good luck, If you still need help feel free to let me know.

Community
  • 1
  • 1
Devin M
  • 9,636
  • 2
  • 33
  • 46
  • How many models should i have? right now i am making Date,Category,Price (price amount & name of price) and Location ( Google Maps API ). Is this OK, especially if i plan on building on top of these resources? – LearningRoR May 27 '11 at 03:10
  • I want a Data Model to make stats out of every piece of information (date,category,cost,location,price name,etc). Do i build models for each part? – LearningRoR May 27 '11 at 03:18
  • 1
    I would just make a model that has these fields. You want to keep things as simple as possible. Why do you want to use a separate model for each field? If you write code like this you are repeating yourself a lot which is not a good practice. – Devin M May 27 '11 at 04:44
  • I was thinking it was a good idea to use a model for each field because the Date,cost,category,location can be used in the future in the Data section of my website for graphing and other calculations in the long run. Is it possible to grab those by just putting all of them in the Price Model instead? – LearningRoR May 27 '11 at 05:01
  • 1
    You could just pull these attributes from the objects in the database. You dont want separate models for this type of information. You can call extract and iterate through this information if it is attached to the model. – Devin M May 27 '11 at 05:05
  • So in conclusion i have 2 models, Category and Price, the category belongs_to Price; this holds the date,cost,name and location. is this good practice? – LearningRoR May 27 '11 at 05:21
  • 1
    no good practice would be Object with the fields date, cost, name, locaiton and a belongs_to relationship to category – Devin M May 27 '11 at 05:36
  • Object? hmm I'm lost now, i was going to set up like this User model -> Price model -> Category model. Price belongs to User, Category belongs to Price. – LearningRoR May 27 '11 at 05:44
  • 1
    How would that be useful? Why would you have this crazy inheritance? Objects have properties, when these objects need to belong to another complex object (for example a post belonging to a user) then you use a relation. If you have an object that needs to use a price and category you don't just use assiocations for that. – Devin M May 27 '11 at 05:47
  • Well as i see it if Users were to add Prices, i make the Price Model and controller as you said, but if i wanted to let Users create or pick their own Categories i would need a new model i thought? When you write Object i get confused if you mean Object Model or just an object(name?), sorry I'm pretty brand new to everything programming. – LearningRoR May 27 '11 at 05:53
  • Usually an object represents an entire thing. What are you trying to model anyway? Perhaps I can give you a better example then. – Devin M May 27 '11 at 20:18
  • Lets say i generate a scaffold: rails generate scaffold Price name:string date:date cost:decimal category:text. Would this be OK? my main goal is to make it so users can add their own prices on my website and save certain prices in custom categories. – LearningRoR May 27 '11 at 21:13
  • As long as the only object you want is a Price. Is that the whole object or are you going to try to tie price to a Product or something else? – Devin M May 27 '11 at 22:20
  • Nope, Price is the only object just like if a User was to make Post. It is the main core of my website. – LearningRoR May 27 '11 at 23:07
  • Alright continue on, I was worried that you were just chaining associations together. – Devin M May 28 '11 at 00:40
  • Alright thanks for everything, I'm on the right track now, thank you! – LearningRoR May 28 '11 at 03:58