Routing problem in Rails 2.x

Hi

I get this error:

Showing workouts/show.html.erb where line #14 raised:

undefined method `activities_path’ for #ActionView::Base:0x720828
Extracted source (around line #14):

11:
12:

Add an exercise to this Workout


13:

14: <% form_for(:activity, :url => activities_path(@workout)) do |f|
%>
15: <% render :partial => ‘activities/form’, :locals => {:f =>
f}%>
16: <% end %>
17:

The top of my routes.rb looks like this:

ActionController::Routing::Routes.draw do |map|

map.resources :workouts do |workout|
workout.resources :activities
end
.
.

Can anyone give me a clue about what I’ve done wrong?

Edward

Try for
<% form_for @activity do |form| %>

:slight_smile:

Bala

'Fraid that doesn’t work. The ‘add activity’ is part of the Workout
show page and the @activity in your code is nil.

On Sep 9, 12:41 pm, “bala kishore pulicherla” [email protected]

'fraid that doesnt work either.

NoMethodError in Workouts#show

Showing workouts/show.html.erb where line #15 raised:

undefined method `nil_class_workout_path’ for #<ActionView::Base:
0x1637cf8>

Extracted source (around line #15):

12:

Add an activity to this Workout


13:

14:
15: <% form_for [@activity, @workout] do |f| %>
16:
17: <% render :partial => ‘activities/form’, :locals => {:f =>
f}%>
18: <% end %>

i thought an activity has_many workouts :frowning:

itz in the reverse way :slight_smile:

try for
<% form_for [@activity, @workout] do |form| %>

hope it will work
:slight_smile:

Bala

:o

Do u did before_filtter for the workut to load the activity???

if not so do that in this way

i assume the fallowing relation ship b/w models depending on ur routes

Model Workout
has_many :activities

Model Activity
belongs_to :workout

In the workout controller i assume

def new
@workout= Workout.new

respond_to do |format|
  format.html # new.html.erb
  format.xml  { render :xml => @workout}
end

end

def create
@workout= Workout.new(params[:workout])

respond_to do |format|
  if @workout.save
    format.html { redirect_to(@workout) }
    format.xml  { render :xml => @workout, :status => :created,

:location => @workout}
else
format.html { render :action => “new” }
format.xml { render :xml => @workout.errors, :status =>
:unprocessable_entity }
end
end
end

in ur activity controller

before_filter :find_workout

def new
@activity= Activity.new
end

def create
@activity = Activity.new(params[:activity])
if (@workout.activities << @activity)
redirect_to workout_url(@workout)
else
render :action => :new
end
end

private
def find_workout
@workout_id = params[:workout_id]
return(redirect_to(workouts_url)) unless @workout_id
@workout= Workout.find(@workout_id)
end

and ur view file for activity/new
<% form_for [@workout, @activity] do |form| %>

Add an Activity

<%= render :partial => ‘form’, :object => form %>

<%= submit_tag "Create" %>

<% end %>

*and the link for the new activity will be

<%= link_to “Add Activity”, new_workout_activity_url(@workout) %>
*

*and ur routes

map.resources :workouts do |workout|
workout.resources :activities
end*

If still any issues :o let us know :slight_smile:

:slight_smile:
Bala Kishore

simply changing this

form_for(:activity, :url => activities_path(@workout))

to

form_for(:activity, :url => workouts_activities_path(@workout))

should work