Topic: Help with Subroutes [solved]

Hi guys!
I am at a loss... For some reason my route is not working.
Basically I have two models: Form and Line.
Form has_many Lines
and Lines belongs_to Form.
Now my routes.rb says:

ActionController::Routing::Routes.draw do |map|
  map.resources :forms
  map.resources :lines
  map.resources :form, :has_many => [:lines]
  map.connect ':controller/:action'
end

Now my regular form show works fine:
http://localhost:3000/forms/1/

but when I try to create a new line for a particular form:
http://localhost:3000/forms/1/lines/new

I get the error:
Routing Error

No route matches "/forms/1/line/new" with {:method=>:get}


Any ideas would be greatly appreciated!

Last edited by drozzy (2008-03-16 16:56:17)

"Iwana ga hana"

Re: Help with Subroutes [solved]

I think i kinda of got it.
Is it something like:

  map.resources :lines
  map.resources :forms, :has_many => [:lines]

Also I guess I have to tune all my controller code too.

"Iwana ga hana"

Re: Help with Subroutes [solved]

Hm.. im' still having a problem, because no matter what form I display the Lines for, it always shows the same lines. So:

http://localhost:3000/forms/1/lines

and
http://localhost:3000/forms/2/lines

Show exactly the same lines...

"Iwana ga hana"

Re: Help with Subroutes [solved]

ook at your database and make sure sure forms_id=1 for those lines that are affiliated with forms/1 and make sure forms_id = 2 for those lines associated with forms/2.  Also, make sure that your lines controller's index action is loading up lines association with params[:form_id] -- instead of ALL lines.

In your index action of your LinesController:

First find the form in question.

@form = Form.find(params[:form_id])

  then get the lines like this

@lines = @form.lines

Re: Help with Subroutes [solved]

Oh ya, that's the stuff!

"Iwana ga hana"

Re: Help with Subroutes [solved]

Hm.. now i have another newb problem.
Whenever I create a new Line - it doesn't get form_id set automatically.
Where do i need to do that?
I assume in create action of the line:

# When going to URL:
http://localhost:3000/forms/16/lines/new

# Create Action in Lines controller
@form = Form.find(params[:form])
@line = Line.new(params[:line])
@form.lines.new ??????
if @line.save
        flash[:notice] = 'Line was successfully created.'
        format.html {redirect_to form_path(@form) }

end


i'm confused...

"Iwana ga hana"

Re: Help with Subroutes [solved]

I found this blog very useful, it deals with rails 2 and sub-resources:
http://railsonedge.blogspot.com/2008/02 … -part.html

"Iwana ga hana"