Let's say I have a User
model, and an Invoice
model with a belongs_to :user
association.
Now I'm creating a new
action for my InvoicesController
, and the view that will be rend开发者_如何转开发ered. The view will have a select-element for selecting the user that this invoice will belong to.
So I need to fetch those users somewhere; my instinct is to leave this kind of thing out of the view. I end up with this:
def new
@users = User.all
end
The form submit action is create
. When the creation fails for some reason, I re-render the new
action's view.
def create
invoice = Invoice.new params[:invoice]
if invoice.save
flash[:notice] = 'Invoice created!'
redirect_to :action => 'show', :id => invoice.id
else
@users = User.all
render :action => 'new'
end
end
But as you can see, in order the re-render the new
action, I have to fetch the users again.
This is just an example, but consider that I have some forms with several select-elements filled from the database, or similar constructs. That turns into an awful lot of repetition.
So how should I structure my controller in this situation?
- Should I simply use
User.all
from my view? - Should I call
new
from withincreate
to do the fetching for me? - Or something else?
For this I'd use a before_filter
. For example you'd do something like:
before_filter :fetch_all_users, :only => [:new, :create]
protected
def fetch_all_users
@users = User.all
end
For 90% of my controllers I use the inherited resources plugin. It cuts down the amount of controller code you need to write for CRUD controllers, which also means you can cut down on the amount of tests you need to write.
For me:
What's the rails way to load other models collections for new, edit update and create actions?
It's not a good approach for my situation. Where after ".save", I send redirect_to to an another action, if I use before_filter and ".save" returns true, the fetch_all_users is called unnecessary
精彩评论