An error adding activities.
|
|
I’m about halfway through the Exercisr. I am at the point where I’m trying to add an activity to a workout. Everything seems to be fine until I attempt to Save the activity. (Sorry, but I am using more current versions that what is prescribed in the book: I get this error message: Couldn't find Workout without an ID from this application trace: /home/phil/.gem/ruby/1.8/gems/activerecord-2.3.2/lib/active_record/base.rb:1572:in `find_from_ids' /home/phil/.gem/ruby/1.8/gems/activerecord-2.3.2/lib/active_record/base.rb:616:in `find' /home/phil/.gem/ruby/1.8/gems/activerecord-2.3.2/lib/active_record/associations/association_collection.rb:60:in `find' /home/phil/www/exercisr/app/controllers/activities_controller.rb:92:in `find_workout' I’m a little confused as to where the workout_id comes from in the protected method find_workout in the activities_controller. My model objects looks like this:
class Workout < ActiveRecord::Base
belongs_to :user
has_many :activities, :dependent => :destroy
has_many :exercises, :through => :activities
validates_presence_of :date
end
class Activity < ActiveRecord::Base
belongs_to :exercise
belongs_to :workout
validates_presence_of :resistance, :repititions
attr_accessor :new_exercise_name
before_save :create_exercise_if_submitted
def create_exercise_if_submitted
create_exercise(:user_id => workout.user_id, :name => new_exercise_name) unless new_exercise_name.blank?
end
end
(I know repetition is spelled incorrectly, but it is incorrect consistently within the application). Can anyone point me in a direction where to look as to why the find_workout is not happening? Is it that I am somewhere somehow not passing a workout object to the activity controller? |
|
|
mongo, if you want to zip up the application and email it to me, I’ll take a look at this weekend and let you know what the issues are. —Eldon |
|
|
Eldon, Thank you so much for taking the time to look at this, and the most helpful book. Tell your publisher there is an increasing demand for an updated version… I have zipped up the exercisr directory and emailed it to an alameda eldon gmail account. I’m using mysql, so no db is included in the file. Please don’t spend a whole lot of time looking at this, as me struggling to figure it out is all part of the learning experience. I do appreciate any tips or pointers in the general direction to look. If I understand the concepts correctly, things like workout_id are automatically generated and set up based upon the relationships defined in the models of the related objects. In this case between an activity and a workout. I’m sure its something simple. |
|
|
I’m having exactly the same problem. Any ideas? Thanks Gruff |
|
|
I fixed it. The tutorial code is wrong in several places. If you run “rake routes” you can inspect the routes created and also the named path helpers. The code is specifying the wrong helpers in a few places. 1. In the the app/views/show.html.erb the :url should be HTH Gruff |
|
|
Sorry, my post got munged as I didn’t escape my code samples. |
|
|
There are two file changes needed to get it to work. 1. change the _activity partial to look like:
<tr>
<td><%= activity.exercise.name %></td>
<td><%= activity.repetitions %></td>
<td><%= activity.resistance %></td>
<td><%=link_to image_tag("edit_photo.gif", {:title => "Edit Exercise"}), edit_workout_activity_path(@workout, activity) %></td>
<td><%= link_to image_tag("delete_photo.gif", {:title => "Delete Exercise"}), workout_activity_path(@workout, activity),
:confirm => 'Are you sure?',
:method => :delete %> </td>
</tr>
|
|
|
and change the workout show view to look like:
<h1><%= h @workout.label %> Workout on <%= h @workout.date.to_s(:long) %> </h1>
<table>
<tr><th>Exercise</th><th>Reps</th><th>Resistance</th></tr>
<%= render :partial => 'activities/activity', :collection => @activities %>
</table>
<h3>Add Exercise to this Workout</h3>
<% form_for(:activity, :url => workout_activities_path(@workout)) do |f| %>
<%= render :partial => 'activities/form', :locals => {:f => f} %>
<% end %>
<%= link_to 'Back', workouts_path %>
|
|
|
I think you can also use a more concise method on config/routes.rb to map subordinate routes like this: map.resources :workouts :has_many => :activities instead of map.resources :workouts do |workout| workout.resources :activities end |
|
|
No, it seems I lied about the routes thing. Read it somewhere, maybe it’s an edge rails thing. |
|
|
Crap – I got so swamped at work that I completely forgot about this problem. So my apologies to Mongo for not looking into this. If GruffDavies is correct and it’s simply a matter of the named route helpers that are generated are incorrect that would make sense. This book was written using Rails 1.2.6, and there were some minor changes to those routes in the early 2.x days. One of which was the automatic pre-fixing of nested routes with the parent routes name. And Gruff – yes you should be able to use the shorthand version of :has_many in route generation now as well (also added to Rails after the book was published). You can use the shortcut has_one to link to a singular nested resource and a has_many to link to a collection nested resource. (However – I will note that I rarely see this idiom used). As for why it wasn’t working for you – I think you’re missing a comma between :workouts and :has_many. Hope that helps – once again sorry for forgetting about this. |
|
|
Hello. I am actually having this exact same problem. I’ve been looking at this error for 3 hours, and really, even though I’ve made some progress, I’m frankly at a loss. I know this topic is about a year old… but can I still get some input? The error is: Couldn’t find Workout without an ID and my routes looks like this: bq.(ActionController::Routing::Routes.draw do |map| map.resources :activities
|