ruby on rails - How should I structure controller actions that share templates? -


Assume that I have a user model, and a invoice Model related_to: user with association.

Now I am creating a new action for my InvoicesController , and the scene will be sung, a selection of scenes for this user's selection- Element will be from this invoice.

So I need to bring those users somewhere; My tendency is to leave out seeing this kind of thing, I end up with:

  def new @users = User.all end  

form Submit create . When creation fails for some reason, I render the view of the action of new action.

  def invoice = invoice. New Perm [: Invoice] If Invoice. Flash [: notice] = 'invoice created!' Redirect_to: Action => 'Show' ,: ID = & gt; Invoice.id other @ user = user.all render: action = & gt; The end of the 'new' end  

But as you can see, in order to reproduce the new action, I have to bring users back .

This is just an example, but keep in mind that I have some forms with many select elements filled with databases, or similar constructions

  • am I just User.all

    > In my opinion?

  • Should I call new to create to fetch for me?
  • Or something << li>

For this I before_filter was using. For example, you do something like this:

  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 plugin It requires the controller to write for CRDUD controllers Reduces the amount of code, it also means that you can cut the necessary tests to write.


Comments

Popular posts from this blog

sql - dynamically varied number of conditions in the 'where' statement using LINQ -

asp.net mvc - Dynamically Generated Ajax.BeginForm -

Debug on symbian -