Forum: Ruby on Rails start_form_tag not taking url_for options

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Pat M. (Guest)
on 2006-01-19 14:07
(Received via mailing list)
I've got a form that should post to user/login.  The code to start the
tag is:
<%= start_form_tag { :controller => "user", :action => "login" } %>

That gives me this exception:
compile error
script/../config/../app/views/user/login.rhtml:1: parse error,
unexpected tASSOC, expecting '}'
_erbout = ''; _erbout.concat(( start_form_tag { :controller => "user",
:action => "login" } ).to_s); _erbout.concat "\n"
                                                              ^
script/../config/../app/views/user/login.rhtml:1: parse error,
unexpected ',', expecting '}'
_erbout = ''; _erbout.concat(( start_form_tag { :controller => "user",
:action => "login" } ).to_s); _erbout.concat "\n"



However if I do:
<%= start_form_tag({ :controller => "user", :action => "login" }) %>

it just spits out a form <form action="/">

Not sure what's going on..I need to be able to specify the controller
and action that the form posts to.

Pat
Jonathan V. (Guest)
on 2006-01-19 15:01
My guess would be that the first example is giving an error because it
thinks you are passing a code block, not a hash, and the code block has
syntax errors.

This should work:

<%= form_tag :controller => 'user', :action => 'login' %>

Cheers, Jonathan.

Pat M. wrote:
> I've got a form that should post to user/login.  The code to start the
> tag is:
> <%= start_form_tag { :controller => "user", :action => "login" } %>
>
> That gives me this exception:
> compile error
> script/../config/../app/views/user/login.rhtml:1: parse error,
> unexpected tASSOC, expecting '}'
> _erbout = ''; _erbout.concat(( start_form_tag { :controller => "user",
> :action => "login" } ).to_s); _erbout.concat "\n"
>                                                               ^
> script/../config/../app/views/user/login.rhtml:1: parse error,
> unexpected ',', expecting '}'
> _erbout = ''; _erbout.concat(( start_form_tag { :controller => "user",
> :action => "login" } ).to_s); _erbout.concat "\n"
>
>
>
> However if I do:
> <%= start_form_tag({ :controller => "user", :action => "login" }) %>
>
> it just spits out a form <form action="/">
>
> Not sure what's going on..I need to be able to specify the controller
> and action that the form posts to.
>
> Pat
Pat M. (Guest)
on 2006-01-19 15:07
(Received via mailing list)
When I do that, it's still generating the wrong form tag:
<form action="/" method="post">
Kevin O. (Guest)
on 2006-01-19 15:13
Pat M. wrote:
> When I do that, it's still generating the wrong form tag:
> <form action="/" method="post">

What do you get if you just stick in a

url_for :controller=>'user', :action=>'login'

?

_Kevin
Pat M. (Guest)
on 2006-01-19 15:22
(Received via mailing list)
On 1/19/06, Kevin O. <removed_email_address@domain.invalid> wrote:
> _Kevin
>
>
> --
> Posted via http://www.ruby-forum.com/.
> _______________________________________________
> Rails mailing list
> removed_email_address@domain.invalid
> http://lists.rubyonrails.org/mailman/listinfo/rails
>

I just tried:
<form action="<%= url_for :controller=>'user', :action=>'login' %>"
method="post">

instead of using start_form_tag, and I get the same thing still..I
wonder if webrick might be caching it or something?  I've cleared the
cache on my browser multiple times, used a different browser, and even
tried connecting from a different computer.  I haven't turned any
caching on in rails, so unless it's on by default somewhere, I don't
know why anything would be cached.

Pat
Pat M. (Guest)
on 2006-01-19 15:31
(Received via mailing list)
On 1/19/06, Pat M. <removed_email_address@domain.invalid> wrote:
> >
>
>
> Pat
>

Okay I might be getting a little bit closer...
url_for :controller => 'user', :action => 'login' apparently returns '/'

If I change the controller to anything else, I get the expected url.
If I change the action to anything else, I get the expected url.  Just
can't use this combo, apparently.

Now I think I've really figured it out..in my routes.rb file I have:
map.connect '', :controller => "user", :action => "login"

So I guess url_for uses routes to generate the urls?  Not sure..is
there any way I can make user/login respond to the app root, and still
be able to use url_for in other places?

Pat
Tom W. (Guest)
on 2006-01-19 15:35
(Received via mailing list)
> I just tried:
> <form action="<%= url_for :controller=>'user', :action=>'login' %>"
> method="post">
>
> instead of using start_form_tag, and I get the same thing still..I
> wonder if webrick might be caching it or something?

To ease your fears, try adding 'id="something"' to your form
declaration - if you see that in the source reaching the browser, you
know nothing is being cached anywhere down the chain.

Assuming caching isn't the issue, what url do you expect to get?  Have
you changed routing.rb to map / to user/login?

Tom
Pat M. (Guest)
on 2006-01-19 15:38
(Received via mailing list)
> Assuming caching isn't the issue, what url do you expect to get?  Have
> you changed routing.rb to map / to user/login?
Yeah, I did..is that what's causing this?  I'm kind of guessing so,
but I didn't know that url_for uses routes to generate the urls.  It
seems as if I map / after the default route, everything works fine:
  # Install the default route as the lowest priority.
  map.connect ':controller/:action/:id'
  map.connect '', :controller => "user", :action => "login"

Does that sound right to you?

Pat
Jarkko L. (Guest)
on 2006-01-19 15:41
(Received via mailing list)
On 19.1.2006, at 14.53, Pat M. wrote:
>
> So I guess url_for uses routes to generate the urls?

Most definitely, that's the (half of) idea behind routing and using
these url helpers. They will change according to your needs. However,
url_for will use the first route that matches, so you can put this
above the route you mention:

map.connect 'user/login', :controller => "user", :action => "login"

That way, both url's map to the same controller but the url
generation in Rails uses the user/login one.

//jarkko

>   Not sure..is
> there any way I can make user/login respond to the app root, and still
> be able to use url_for in other places?
>
> Pat
> _______________________________________________
> Rails mailing list
> removed_email_address@domain.invalid
> http://lists.rubyonrails.org/mailman/listinfo/rails
>

--
Jarkko L.
http://jlaine.net
http://odesign.fi
Tom W. (Guest)
on 2006-01-19 15:44
(Received via mailing list)
On 1/19/06, Pat M. <removed_email_address@domain.invalid> wrote:
> > Assuming caching isn't the issue, what url do you expect to get?  Have
> > you changed routing.rb to map / to user/login?

> Yeah, I did..is that what's causing this?  I'm kind of guessing so,
> but I didn't know that url_for uses routes to generate the urls.  It
> seems as if I map / after the default route, everything works fine:

url_for does use routes to generate urls.  this gives you loads of
flexibility when changing your url schemes, assuming you've used
url_for throughout your app.

>   # Install the default route as the lowest priority.
>   map.connect ':controller/:action/:id'
>   map.connect '', :controller => "user", :action => "login"

Looks good

Tom
This topic is locked and can not be replied to.