Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialRafael Flores
Courses Plus Student 22,056 PointsRails Controllers and views
I have benn trying this all day with out luck I a new at programming and rails my controller for this case is
class VehiclesController < ApplicationController
before_action :set_vehicle, only: [:show, :edit, :update, :destroy]
def index
@vehicles = Vehicle.find(params[:customer_id])
end
The resource shows in routes as nested under customer
resource :customers do
resource :vehicles
end
Of course on the model vehicles belong to customer.
When I try to go to the vehicle index I get a:
Couldn't find Vehicle with 'id'=1
Of course i have not created one vehicle yet, but my question is how do I get rails to still render the page even if empty?
Below what rspec is returning:
Viewing Vehicles index displays no items when there is no vehicles created
Failure/Error: click_link "List Vehicles"
ActiveRecord::RecordNotFound:
Couldn't find Vehicle with 'id'=1
# ./app/controllers/vehicles_controller.rb:7:in `index'
# ./spec/features/vehicles/index_spec.rb:12:in `block (3 levels) in <top (required)>'
# ./spec/features/vehicles/index_spec.rb:11:in `block (2 levels) in <top (required)>'
1 Answer
Maciej Czuchnowski
36,441 PointsOK, this line is wrong:
@vehicles = Vehicle.find(params[:customer_id])
You are basically looking for one vehicle that has the same id as the customer that you pass in the params.
Rafael Flores
Courses Plus Student 22,056 PointsThanks for your help sorry I could not answer before. I am confused I pass the customer_id because that is the foreign key name in vehicles. As vehicle belongs to customer. I just push the app in git please take a look and let me know I am just getting started with rails and programming period.
Maciej Czuchnowski
36,441 PointsI'll do some changes in your code and make a pull request so that you will see exactly what I added or removed, ok?
I assume that with vehicle index action you want to get all the vehicles that belong to the user that gets passed in the url, right?
Maciej Czuchnowski
36,441 PointsReady. You can merge my pull request with your repo. I had to rewrite your whole vehicles controller and some views. Also, you had one bad migration and one corrupted database column. When you pull these changes, you have to do rake db:drop
, rake db:create
and rake db:migrate
to reset the whole database and make it work properly. You have to read up on nested resources in Rails and practice a bit, because your controller code needed a lot of changes to work. I also removed all the json-format code from the controller to make it more readable (it's just noise and I doubt you will be using json format anytime soon).
Maciej Czuchnowski
36,441 PointsMaciej Czuchnowski
36,441 PointsThis is probably something deeper, so you'd need to show us your whole project. Could you publish it on github and link here? By the way, did you consider doing MOOCs on programming, computer science and Rails in particular? They are free, university-level courses online.
https://www.edx.org/course/introduction-computer-science-harvardx-cs50x
https://www.coursera.org/course/webapplications