Problem on page 137

Subscribe to Problem on page 137 13 post(s), 2 voice(s)

 
Avatar Scott Gardner 7 post(s)

At the completion of editing /app/views/workouts/show.rhtml on page 137, I am getting an error when I go to http://localhost:3000/workouts/1

http://pastie.caboo.se/128125

 
Avatar EldonAlameda Administrator 216 post(s)

Hi Scott,

Could you also send a paste of your routes.rb from /config?

Thanks

Eldon

 
Avatar Scott Gardner 7 post(s)

Here you go:

http://pastie.caboo.se/128271

Thanks for your help!

 
Avatar EldonAlameda Administrator 216 post(s)

Scott,

I’m not at home where i can test this but one thing I notice that seems odd is that in your routes.rb on line 7 you do a second call to create a resource for workouts

map.resources :exercises, :users, :workouts

Since i can’t test it from here – I’m wondering if calling it again is essentially “overwriting” the previous one (on line 2) that had the nested activities resource. If that’s the case that would explain why your getting an error that the route doesn’t exist

Could you try changing line 7 to map.resources :exercises, :users and let me know if that helps?

Also could you confirm if you’re using Rails 1.6 or Rails 2.0?

 
Avatar Scott Gardner 7 post(s)

I changed routes.rb. Didn’t help. Same error:

http://pastie.caboo.se/128313

I’m on Rails 2.0.1.

 
Avatar EldonAlameda Administrator 216 post(s)

Ahh….

Rails 2 introduced some changes to the ways that routes are generated and I’m betting that’s the problem that you’re encountering.

In Rails 2 – nested routes are automatically assigned a name prefix to help avoid naming conflicts. So in Rails 2 – the correct route would be workout_activities_path(@workout)

The advantage of this is that you could have both a direct route and a nested route to the activity resource.

So if you want to try and build this project within Rails 2 – then you’ll either have to change all the named routes for activities to include the workout_ named prefix or you could simply add a :name_prefix => nil to the nested activities route.

Let me know if that helps / makes sense

Eldon

 
Avatar Scott Gardner 7 post(s)

So do you mean that I should insert workout_ before every instance of activities_path? I tried that (changes in activities/new and ../show, and workouts/show) which generated…

NoMethodError in ActivitiesController#create

undefined method `new_exercise=’

What does :name_prefix => nil do? I tried adding it to routes.rb…

map.resources :workouts do |workout|
  workout.resources :activities, name_prefix => nil
end

...which generated a NameError undefined…`name prefix’

 
Avatar EldonAlameda Administrator 216 post(s)

It looks like you forgot the colon in front of name_prefix in your route. name_prefix is a symbol and needs the colon in front of it to work. So it should be:

map.resources :workouts do |workout|
  workout.resources :activities, :name_prefix => nil
end

But yeah, otherwise you would need to add that workout_ prefix in front of every occurence of an activities named route (such as activities_path or activities_url) in order for it to work. The project was built in Rail 1.2.6 and all the named routes would need to be converted to the changes in routing that Rails 2.0 introduced if you don’t want to run it in Rails 1.2.6.

A good trick to help you convert the routes is that there’s a new rake task that will show you your routes – just run rake routes from the root of your application.

 
Avatar Scott Gardner 7 post(s)

Tried both. Getting same errors as previously described.

 
Avatar EldonAlameda Administrator 216 post(s)

Scott,

You’re still getting the undefined method `activities_path’ error message?

Obviously there will be some challenges in converting the project to Rails 2.0 but that seems odd that you can’t get past this one. Would you mind zipping up the app in it’s current state and emailing it to my gmail account ( alameda.eldon ) and I’ll take a look at it over the weeekend?

 
Avatar Scott Gardner 7 post(s)

Yes, same error. I sent my version of the app to you. Thanks

 
Avatar EldonAlameda Administrator 216 post(s)

Scott,

Just sent the code back to you and it should be working now.
Added the :name_prefix => nil to the activity route did get us past the the changes in Rails 2.0
The other error that you were getting for new_exercise= was due to a typo in your form – the field name for adding a new exercise to a workout was supposed to be new_exercise_name but you had left off the _name – so when the form posted it didn’t match the accessor in your activity model.

Eldon

 
Avatar Scott Gardner 7 post(s)

Thanks for your help!