Nested RESTful routes in Rails: how should I organize my views? - ruby-on-rails

Lets say you have an Invoice resource, and each Invoice has nested InvoiceLine resources, i.e. a very common nested resource pattern.
Now, where should one put the view for showing each individual invoice line as a row within the invoice? Should invoice lines be rendered as a partial under invoices/_line.html.erb, or should they get their own full-fledged view under invoices_lines/show.html.erb? Normally the former would be the obvious choice, but when you add AJAX functionality (for adding and editing lines in the invoice show view), things get complicated.
I find myself really struggling with this on a regular basis, and I'm not really sure of the right way to deal with it.
Here's what I'm thinking:
A RESTful route for this will likely look something like this:
map.resoures :invoices do |invoice|
invoice.resources :lines, :controller => :invoice_lines
end
The standard folder structure for the views is:
app/
views/
invoices/
invoice_lines/
Now, when rendering the entire invoice with all of its lines, my inclination would be to make each line into a _line.html.erb partial under invoices/. But then when I want to add some AJAX functionality for adding and editing lines, the PUTs and POSTs are routed to the invoice lines controller, so shouldn't the line rows be rendered by a standard show.html.erb view under invoice_lines.erb?
So should my views look like this:
app/
views/
invoices/
index.html.erb
show.html.erb
_line.html.erb
invoice_lines/
Or like this:
app/
views/
invoices/
index.html.erb
show.html.erb
invoice_lines/
index.html.erb
show.html.erb
I'm stuck between having the InvoiceLinesController render its response to AJAX requests using the partial in invoices/_line.html.erb, or I can put the line row template code in invoice_lines/show.html.erb, and have invoices/show.html.erb render the individual lines as a component or AJAX request to InvoiceLinesController#index.
Neither alternative feels right. Any suggestions on the best practice here?
It feels as if invoice lines controller should have it's own set of views when rendered in the context of the invoice, but Rails doesn't really have an obvious provision for "contextualized" views.

What about to put a partial _line.html.erb into the invoice_lines folder and then use this partial in the invoices_lines/show.html.erb template and the invoices/show.html.erb.
That's how I did it in similar situations.
This way all invoice specific view code is in the invoice folder and all invoice_line code is in the invoice_line folder.
You are DRY: you can use Ajax calls to the invoice_line controller or render the whole thing by displaying invoice/show.html.erb.
invoice_items would be rendered both times through the same partial in invoice_lines/_line.html.erb
I hope this makes sense and is sort of an answer to your question. I'm not yet too experienced with Rails so I can't tell if this is "best practice"

Related

Confused about how the render works in rails

I'm still fairly new to rails, but I've been trying to figure it out by building a bunch of simple projects like blogs, forums, clones over the weekend.
One of the blogs that I'm working on is a typical blog with users(devise), posts, and comments. I've been following a tutorial for this.
The thing that I've been really confuse on is setting up the comments and rendering it.
The tutorial teaches me to make a partial for comment in the comment view named _comment.html.erb and then another for form _form.html.erb . I fully understand that to call the form i just do render 'comments/form' but for the comment.html.erb i need to render #post.comments how come it's not 'comments/comment'? Why is it comments with the S?
I've tried reading up on render and plurization on rails, but most of them just talk about the model.
There are two convention over configuration things going on here that may be causing you some confusion but are meant to save time.
Rails will automagically find the right partial if it follows naming conventions, meaning if your model is Comment that the view partial is located in app/views/comments/_comment.html.erb. That means you don't have to specify the location of the partial when calling render, but instead can just pass the model object directly and Rails figures out that you want it to render the partial and finds it on its own.
The reason it's comments plural here is that you are rendering all of the comments as a collection, not just a single comment. It's a convenience feature to allow the developer to simply tell Rails to render a collection and it will automagically know to use the corresponding partial. It's identical to typing:
#post.comments.each do |comment|
render 'comments/comment`, object: comment
end
Note how the above code is calling render directly on a model object so we don't have to bother specifying the location of the partial (again, assuming you followed the convention when naming and locating things). If we named the partial something else, then we'd have to specify the location like your other examples.

Rails 4 Views - Best Practices

I have two controllers, UsersController and TransactionsController and I'm wondering what the best practices is for displaying data from both controllers on one view. Should I create partials under each corresponding views directory and then stitch them together in a separate view or do I create one view under layouts? Am I way off?
EDIT:
In my Views folder:
devise
(devise default views are in here)
layouts
_header.html.erb
application.html.erb
transactions
_form.html.erb
index.html.erb
delete.html.erb
edit.html.erb
new.html.erb
users
index.html.erb
I want to show both all users in one table and their transactions in another on the root page.
If transactions belong to user, you should be able to access transaction data on the user page by default (if models&migrations are set up correctly). If your users index.html.erb will be your root page, that should solve your problem.
If you are trying to show this data in an administration page for example then your code should go like this
Controllers:
admin_controller.rb -> with action users_transactions
views/admin
users_transactions.html.erb -> loop on users showing the partial of user
views/transcations
_user_transactions.html.erb
views/users
_user.html.erb -> here you will show how a single user will show including the partial of his transactions.
So I guess it all depends on 'what' you want to display and 'how'.
Leave the views/layouts Dir for what it was created, storing layouts. Generally cross-view partials are kept in the views/shared Dir, which stores partials that could be rendered from another directories. in your case form transactions/* & users/*.
Plan wisely before adding a file there... maybe you don't really need it ;)

where to place my forms to follow 'DRY'

I am working on a larger application and I see that I end up repeating the same forms twice etc.
For example when you register and on edit account one is in the #new and the other is in the #edit. I'm assuming this is bad practice and it also takes up a lot of space in the view. How do I gather these forms and then just display them on both pages?
If you use the rails generators they normally create a structure like so:
edit.html.erb
new.html.erb
_form.html.erb
Both new and edit pages render the form partial like so:
<%= render 'form' %>
This keeps the code dry so long as your form is the same for editing and creating.
All partial file names begin with an underscore, and if you want a partial to be available across your whole application put it in views/shared.
Some reading on partials.

Rendering different parts of a page from different actions from different controllers

I've got an index page where I've decided to render both Usernames, Posts and other informative sources.
Since Devise takes care of the user creations and login sessions-managing I am aware of needing to have multiple controllers.
PostsController, DashboardController and RandomController. The plan is to make them be rendered on different parts of the page, with different sizes and on different places.
Let's say I've got Post.find(:all => :order => "created_at DESC") in the PostsController. I want to render that someplace on the page.
I want to render Usernames right under the according Post which belongs_to them, and I want to render some still "Random" Controller action which will post news on some random place on the page.
So a recap:
The PostsController finds and renders some part of the index page, and the DeviseControllers I guess renders the username belonging to the according one, and the Random Controller renders some random thing from news or something on the middle of the page. Amongst all of them all of this is happening on another controller called DashBoard.
So all these controller actions are supposed to be rendered on a own DashBoard controller index view.
Any tips, or good ideas on how to accomplish this. I know about partial rendering, but I don't know how to achieve that.
It seems we're talking about a view rendered by Dashboard#index. So all you need to do is define all the info on the page as instance variables under the index action.
Probably the bit of info that's missing for you is that controllers are free to use data from any of your models (and even other classes that don't inherit from ActiveRecord::Base).
For instance, in Dashboard#index, you can have:
#posts = Post.order("created_at DESC").all
Which is referring to a different model than the Dashboard model (assuming one exists).

Two controllers for one shared view in Ruby on Rails

I have two controllers for two respective models, by example, photos and categories. index and show methods are very similar in each controller, and the views are identical. What is the best method for share the view by the two models?
I've though two options:
Use a helper. In the helper will put the code for the view, and will call the helper from each view (photos/views and categories/views)
Use a partial in each views. I think it's a more clean solution, but I see huge DRY's in my mind when going to code this solution.
So, I have two controllers from two models, each one at and exposes a #photo object (photos controller with all the photos, and categories controller with just the selected categorie's photos) and I need one view to show both.
I'm looking for an elegant solution for this, complaining REST and DRY principes. Any idea?
Thanks in advance.
I have a similar situation with one of my projects. All the delete views for most controllers are styled the same way, display the same confirmation boxes, and simply renders a predictable display of whatever object is being deleted.
The solution was quite simple and elegant in my opinion. Simply put, what we (the developers) did was create a new directory in app/views called shared and put shared views in there. These could be full template files or just partials.
I would suggest using a shared template (in neither categories nor photos view directories, but rather in the shared directory) and rendering it manually from the view.
e.g. have a method as such in both controllers and a file app/views/shared/photo.html.erb:
def show
#photo = Photo.first # ... or whatever here
render :template => 'shared/photo'
end
This should successfully render the shared template. It is the DRYest route and doesn't have the feeling of pollution you get when using a more-or-less empty view in each controller's view directory just to include a shared partial, as I understand your question is suggesting.
About the first answer:
If the partial must be rendered from a view:
<%= render :partial => "shared/photo" %>
and the partial must be in app/views/shared/_photo.html.erb
I'd use an helper because they are shared among views. For the HTML part, I'd use partials so it would be a mix of both ways.
This looks like a good use case for the Cells gem.
#bjeanes If all your delete views are the same, you can create views/default/delete.html.erb and all the delete actions will use it.
That's what i'm doing: Most of my views are on default, and i create specific ones only when needed
Update: Ok, this post is from 2009, anyway, i will keep my comment here in case someone gets here from Google like i did.

Resources