Form_for with namespace

Hi,

I’m new to RoR (came over from PHP). I am working on the backend of my
project now, and I’m basically trying to output a form that lets me
create a new ‘design’.

This is what I have in my routes.rb:
map.with_options(:namespace => “admin”) do |admin|
admin.resources :designs
end

In my design controller:
def new
@design = Design.new
end

In my view for the form:
<% form_for([:admin, @design]) do |f| %>
<% end %>

When I attempt to view the form page, I get this error:
undefined method `admin_designs_path’ for #<ActionView::Base:
0x589b760>

I’ve been following the official guides to a T, so I’m not really sure
where I’ve gone wrong. Anyone able to shed some light about this? Many
thanks.

Hi Bryan L wrote:

This is what I have in my routes.rb:
map.with_options(:namespace => “admin”) do |admin|
admin.resources :designs
end

You have to modify your routes as

map.namespace(:admin) do |admin|
admin.resources :designs
end

And also generate controller and model like
script/generate controller Admin::Designs
script/generate model Design #field parameters here

Sijo

Thanks Sijo for the help. I’ve made the changes according to your
suggestions. The error message is now different, but I guess there’s
some improvement!

“undefined method `^’ for “f”:String”

My routes.db:
map.namespace(:admin) do |admin|
admin.resources :designs
end

Any ideas??

My bad… I regenerated the controllers and models and the previous
error seemed to be gone but now I’m getting this error instead:

Called id for nil, which would mistakenly be 4 – if you really wanted
the id of nil, use object_id

If I understand correctly, this statement "<% form_for([:admin,
@design]) do |f| %> " is trying to call the @design model with a nil
id, which results in this error. That seems inevitable as I’m trying
to create a new design through this form.

Thanks Coiln. I deleted that message actually because it slipped my
mind that the line which I defined @design as a Design model got
deleted off when I regenerated the controllers and models. I fixed
that already so now I’m stuck with another different undefined method
error:
undefined method `^’ for “f”:String

Where does this ^ come from? I’ve tried searching through Google and
other ruby forums but seems like I’m the only one having so much
trouble. I guess it might be just something obvious that I just can’t
get for some reason…!

2009/10/6 Bryan L [email protected]:

trouble. I guess it might be just something obvious that I just can’t
get for some reason…!

I think a lot of people have had this problem, I believe it is a known
bug in Rails 2.3.4. I understand the easiest solution is to go down
to 2.3.3 if possible.

Colin

2009/10/6 Bryan L [email protected]:

to create a new design through this form.
If the error occurred on that line then it suggests @design is nil
(@design is not a model it is a variable). You should set this up in
your ‘new’ action before displaying the view. Something like @design
= Design.new.

If this does not make sense then, if you have not done so already,
have a look at the rails guide Getting Started. See

Colin

2009/10/6 Bryan L [email protected]:

<% form_tag do(:method => “get”) %>
Doing this is fine…

<% form_tag do %>
But this gives me the Undefined Method ‘^’ error once more.

Not sure why, but the only difference is that one is a get request,
the other is a post. Surely this can’t be broken as well? Seems like
it may be one of the most basic functions in Rails, and if it were to
be broken, too many people would have been affected by it already.

I think it is only an issue with Ruby 1.9 (with rails 2.3.4), but this
is only from memory, think a bit more googling (or searching the
archives of this list) would be needed to confirm this.

Colin

To anyone who is experiencing this problem, there’s a patch to rectify
this already.

https://rails.lighthouseapp.com/projects/8994/tickets/3144-undefined-method-for-string-ror-234

The latest master branch at github should fix this too! Phew…

On Oct 6, 11:19 pm, Colin L. [email protected] wrote:

I think a lot of people have had this problem, I believe it is a known
bug in Rails 2.3.4. I understand the easiest solution is to go down
to 2.3.3 if possible.
Colin

What about generating normal forms? I can’t seem to do it without
errors.

<% form_tag do(:method => “get”) %>
Doing this is fine…

<% form_tag do %>
But this gives me the Undefined Method ‘^’ error once more.

Not sure why, but the only difference is that one is a get request,
the other is a post. Surely this can’t be broken as well? Seems like
it may be one of the most basic functions in Rails, and if it were to
be broken, too many people would have been affected by it already.