Forum: Ruby on Rails routing and forms

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.
4fded36fc156cec081fe813b1ae8ad1d?d=identicon&s=25 Andrew Yearp (mad4dos)
on 2005-12-07 17:44
please can some one explain how to solve this problem ..

how do you route the http get data from a form  currently it produces a
url like this > http://10.1.0.10/admin/view/7?number=1

i would like it be like this > http://10.1.0.10/admin/view/7/1
how could i do this ?

andrew
3ccecc71b9fb0a3d7f00a0bef6f0a63a?d=identicon&s=25 ksruby (Guest)
on 2005-12-07 18:32
(Received via mailing list)
Add to your routes

map.connect ":controller/:action/:id/:number", :controller => 'admin',
:action
=> 'view', :id => /^\d+$/, :number => /^\d+$/

Kent.
4fded36fc156cec081fe813b1ae8ad1d?d=identicon&s=25 Andrew Yearp (mad4dos)
on 2005-12-07 18:46
ksruby wrote:
> Add to your routes
>
> map.connect ":controller/:action/:id/:number", :controller => 'admin',
> :action
> => 'view', :id => /^\d+$/, :number => /^\d+$/
>
> Kent.


thanks that's solved part of the problem  i can now enter the url
manualy

the thing forgot to say was  that im trying to use a dropdown box on a
form to the  generate the url ie  you select the item number for the
drop down but when i submit the form i would like the url to come out as
:controller/:action/:id/:number(http://10.1.0.10/admin/view/7/2)   but
it come up as http://10.1.0.10/admin/view/7/1?number=2

any idea's how to sovle that bit?
3ccecc71b9fb0a3d7f00a0bef6f0a63a?d=identicon&s=25 ksruby (Guest)
on 2005-12-07 19:09
(Received via mailing list)
I don't know how your form looks like, but if you have this rule in your
routes

map.connect 'admin/view/:id/:number', :controller => 'admin', :action =>
'view', :id => /^\d+$/, :number => /^\d+$/

then

<%= url_for :controller => 'admin', :action => 'view', :id => 3, :number
=> 4
%>

generates

http://your-site/admin/view/3/4

Kent.
4fded36fc156cec081fe813b1ae8ad1d?d=identicon&s=25 Andrew Yearp (mad4dos)
on 2005-12-07 19:22
ksruby wrote:
> I don't know how your form looks like, but if you have this rule in your
> routes
>
> map.connect 'admin/view/:id/:number', :controller => 'admin', :action =>
> 'view', :id => /^\d+$/, :number => /^\d+$/
>
> then
>
> <%= url_for :controller => 'admin', :action => 'view', :id => 3, :number
> => 4
> %>
>
> generates
>
> http://your-site/admin/view/3/4
>
> Kent.

if it's any help here's the code for the form :

<%= form_tag({ :action => "view", :id => @chapter.fanfics_id  },{:method
=>"get",:name  =>"form1"}) %>

<select id="number" name="number" onchange="form1.submit()">
<%=options_from_collection_for_select(@menu, "number", "title",
selected_value =[@chapter.number,@chapter.title]) %>
</select>
<%= end_form_tag %>

and the controller :
  def view
  id = ((params[:id]))
  num = @params["number"]
  if num == nil
   num = 1
   else
   end
  @number=num

  @chapter = Chapter.find_by_fanfics_id_and_number((params[:id]),num)

  @menu = Chapter.find_by_sql [" SELECT * FROM chapters WHERE fanfics_id
= ?",id]

  end
end
3ccecc71b9fb0a3d7f00a0bef6f0a63a?d=identicon&s=25 ksruby (Guest)
on 2005-12-07 19:34
(Received via mailing list)
Well, in this case form_tag method's already generated the url for the
submit
action. It doesn't know about the :number part. So in this case Rails
can't
help you, it's your browser that appends ?number= query parameter. You
can
change form action with javascript though.

Kent.
4fded36fc156cec081fe813b1ae8ad1d?d=identicon&s=25 Andrew Yearp (mad4dos)
on 2005-12-07 19:43
ksruby wrote:
> Well, in this case form_tag method's already generated the url for the
> submit
> action. It doesn't know about the :number part. So in this case Rails
> can't
> help you, it's your browser that appends ?number= query parameter. You
> can
> change form action with javascript though.
>
> Kent.

ok thanks any way

is there any way of getting the same effect with out useing forms
 or  would it be best to replace the dropdown box with a row of
hyperlinks
This topic is locked and can not be replied to.