Skip to content

Instantly share code, notes, and snippets.

@take
Created October 7, 2012 17:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save take/3849060 to your computer and use it in GitHub Desktop.
Save take/3849060 to your computer and use it in GitHub Desktop.
rails, about master data
Takehiro: like theres a table which contains all the state ( = states table )
Takehiro: and theres User table
Takehiro: which contains state_id
Takehiro: when u create a new user, u need to get all the state data to show a select box or something right
pduersteler: Takehire yes. for that you can provide a :collection to your select
Takehiro: oh
Takehiro: kk thx :P
pduersteler: :)
Takehiro: so if i provide :collection to my select, when i do @user = User.new
Takehiro: i get all the data from states aswell?
pduersteler: Takehiro nope
pduersteler: you e.g. do another @states = State.all and then do :collection => @states in your form
pduersteler: or use collection State.all directly in the view, do you use form_for or some gem like formtastic?
Takehiro: oooo kk
Takehiro: i use form_for
pduersteler: Takehiro http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html
Takehiro: well i wanted to ask that is it desirable to get the states data insdie the view like State.all(in view) or do @states = State.all inside the controller
pduersteler: also have a look at the bottom at collection_select
pduersteler: Takehiro basically, MVC says you should prepare your date in the Controller
Takehiro: yea, but if i do State.all inside the view, i wont have to do @states = State.all at new method and edit method right
pduersteler: You just need to make sure to also get the data in your create action and not only in the new action if you're rendering :new again on errors in your create action. if you place it directly in your view, you don't have that dependency
pduersteler: yes, right.
Takehiro: oooo thx for the info about create action
pduersteler: i think if it is something small like State.all you can put it in the view, but if you need to prepare more data, you should put it into the controller, via before_filter
pduersteler: for new, create, edit, update then
Takehiro: i c
pduersteler: at your own discretion ;)
Takehiro: i guess before_filter(when complexe data) or getting it from the view(when simple) might be good
Takehiro: :D thx!
Takehiro: really helped me
pduersteler: you're welcome
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment