i`ve a theoretical question on how to implement the MVC concept the best way in Ruby (as i am more familiar with non-MVC -thx for the hint- languages like ASP, PHP).
In my example app you have to manage a Car and its Parts. Following this I would (in my opinion) implement the following two controllers:
– CarsController
– PartsController
Both have its default actions (index,show,edit,delete).
In the “index” action of the PartsController all linked parts for the car are displayed.
In the “index” action of the CarsController all car details are getting displayed and additionally I would include the content of the “index” action of the PartsController passing the cars id to it (or those in session).
Example layout
(CarsController/show/1)
– Car: Porsche GT
– Brand: Porsche
– Model: GT
– PS: 400
– Parts (coming from PartsController/index, parts of car were cached by CarsController before):
— Doorlock
— Engine 400
I tried using
render "cars/index"
render :action => "cars/index"
render :template => "cars/index"
render :controller => "cars", :action => "index"
in my index.html.erb template but no one of them works (I everytime get the error message “template is missing”).
Because I had a lot of trouble with this already I think my concept/implementation may be wrong.
Any suggestions on this to find a better way ?
Thank you for your help !
UPDATE:
Okay here is a more specific example.Lets say the user goes to localhost/cars/1. Here are the templates of my controllers for doing this:
app/view/cars/show.html.erb
<%= form_for(@car) do |f| %>
<div class="field">
<%= f.label :brand %><br />
<%= f.text_field :brand %>
</div>
<div class="field">
<%= f.label :model %><br />
<%= f.text_field :model %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
<%= render "parts/index" %>
app/view/parts/index.html.erb
<table>
<% @parts.each do |part| %>
<tr id="part-<%= part.id %>">
<td><%= part.amount %></td>
<td><%= part.price_total %></td>
</tr>
<% end %>
</table>
Parts are loaded from cache therefore no car id needed for PartsController (not yet)!
routes.rb
resources :cars do
resources :parts
end
Then i`ll get the following error:
Missing partial parts/index with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee]}. Searched in:
* “D:/Dev/Ruby/MyCRM/app/views”
* “C:/Ruby193/lib/ruby/gems/1.9.1/gems/datagrid-0.5.3/app/views”
It like what you’re trying to do is called ‘rendering a partial’ in Rails.
The basic idea is, you have a stub piece of html (in this case, a parts template to render a list of parts for a certain car), and that piece gets included in one or more full pages.
isn’t working, because it’s looking for a partial named ‘index’ under the parts folder, not a full page. By convention, partials in Rails are named with a leading underscore.
To get this to work, you should create a partial, i.e., app/views/parts/_list.html.erb
The second part is, that partial will need to have access to the parts list. Unless your car controller creates an @parts variable, this will need to be deduced. The way this is done in practice is by passing local variables. So, you’d rewrite _list.html.erb to use ‘parts’ instead of ‘@parts’, then in your car controller, you’d render it like this:
in your index for parts, if you want to list all the parts, you’d render the partial like this:
for more information, and a more detailed walkthrough, read:
http://guides.rubyonrails.org/layouts_and_rendering.html#using-partials