Passing parameters with RESTful urls

Hi,

I’m using RESTful urls for a person model, such as edit_person_path
and new_person_path. The problem I am having is that I want to pass
some parameters with some of these urls:
I want to pass the current person id to the new person_url, as the new
person is to be a child of the current person. I keep getting an error
when I try
new_person_path(@person.id)

Is this because it is not a POST request?

I would also like to set an instance variable in the new action
@person_id = @person.id

I want this to then be passed with the form to the create action. In
the form, I am using the new Rails 2.x syntax:
% form_for @person do |f| %>

In the past I used:
<% form_for(:person, :url => create_person_path(:person_id =>
@person_id)) do |f| %>

How would I first of all make this variable available to the new
action and then pass it on to the create action?

thanks,

DAZ

when I try
new_person_path(@person.id)

if you use additional parameters you must give them names like:

new_person_path(:person_id => @person.id)

to hand additional parameters with forms i would use a hidden_field

Thanks Thorsten,

I can now pass an extra parameter with my new_person_path.

I could now use a hidden form field, as suggested, but in the past, I
have simply been able to pass extra parameters through the form_for
tag like so:

<% form_for(:person, :url => create_person_path(:person_id =>
@person_id)) do |f| %>

Is there anyway I can still do this as it would be preferable to using
a hidden form field?

I’ve tried:

<% form_for @person, :person_id => @person_id do |f| %>

but it doesn’t work. It seems that I am simply trying to do the same
thing as I did with the new_page path. Unfortunately there is now a
bit of ‘rails magic’ going on in the background. It is making it a
‘put’ request because it is in the ‘new’ view. How do I add extra
parameters to these ‘magic’ form_fors?

DAZ

On May 5, 6:18 pm, Thorsten M. [email protected]

I believe this should work:

form_for :person, :url=>people_path(:person_id=>@person_id),
:method=>:post

This was the actual code that worked (it needed the singular
person_path rather than the plural people_path, and this required a
parameter of @person):

<% form_for @person, :url=>person_path(@person, :person_id =>
@person_id),:html => { :method => :post } do |f| %>

Thanks for all the help everybody,

DAZ

DAZ,

Are you using acts_as_tree for this?

acts_as_tree allows you to define parent_id, rather than people_id, and
then
defines methods on your objects like #children to see all the children
of a
certain object.

On Mon, May 19, 2008 at 12:45 AM, DAZ [email protected] wrote:

DAZ

a hidden form field?

new_person_path(:person_id => @person.id)

to hand additional parameters with forms i would use a hidden_field

Posted viahttp://www.ruby-forum.com/.


Ryan B.
http://www.frozenplague.net
Feel free to add me to MSN and/or GTalk as this email.

Hi Ryan,

I’m actually using the betternestedset plugin. This has the same
methods as acts_as_tree, I’m over-riding the column name parent_id
with people_id, so it still works.

The difference with this plugin is that you can’t just do
Person(1).children.new, you need to create a new peson, then move it
to be a child of a certain person. This means that the parent’s id
needs to be continually passed through the new and create process (as
far as I can see…)

The method I mentioned above is the best way I could find of doing
this. Have you got any alternatives?

cheers,

DAZ

On May 18, 11:06 pm, “Ryan B. (Radar)” [email protected]

Restful routing seems to be an easier solution,

map.resources :parents, :controller => “people” do |parent|
parent.resources :children, :controller => “people”
end

Then you can do stuff like new_child_parent_path(1) to go to
/parents/1/children/new

It presents you with a prettier URL whilst still passing in
params[:parent_id] like you want it to.

On Mon, May 19, 2008 at 6:53 PM, DAZ [email protected] wrote:

needs to be continually passed through the new and create process (as
wrote:

Thanks for all the help everybody,

@person_id)) do |f| %>
same

Posted viahttp://www.ruby-forum.com/.


Ryan B.http://www.frozenplague.net
Feel free to add me to MSN and/or GTalk as this email.


Appreciated my help?
Reccommend me on Working With Rails
http://workingwithrails.com/person/11030-ryan-bigg

But will this nested route work recursively? What if I wanted to do

Abraham is a parent of homer who is a parent of Bart, could I do:

parents/abraham/children/homer/children/new ???

I like the sound of using nested RESTful routes and will have a look
at them. Could I nested routes with the same name, ie:
map.resources :people do |parent|
parent.resources :people
end

It would also be good if I could get rid of having to put ‘people’ in
front of all the urls, so I could just use paths like:
people/abraham/homer/bart

This way, the url shows the family tree…

Thanks again Ryan,

DAZ

On May 19, 10:42 am, “Ryan B. (Radar)” [email protected]

No you can’t do that unfortunately. I think with your plugin you should
already have a method to get the ancestors of a perosn.

On Mon, May 19, 2008 at 8:30 PM, DAZ [email protected] wrote:

parent.resources :people
DAZ

/parents/1/children/new

I’m actually using the betternestedset plugin. This has the same
this. Have you got any alternatives?

This was the actual code that worked (it needed the singular
On May 6, 3:23 pm, AndyV [email protected] wrote:

Is there anyway I can still do this as it would be preferable
thing as I did with the new_page path. Unfortunately there is
[email protected]>

to hand additional parameters with forms i would use a
Reccommend me on Working With Railshttp://
workingwithrails.com/person/11030-ryan-bigg


Appreciated my help?
Reccommend me on Working With Rails
http://workingwithrails.com/person/11030-ryan-bigg

cheers Ryan - yest I’ve used the Person.ancestors method to then
create a path that can be used to create an url that looks like a
family tree. This involved over-riding the RESTful urls using:

map.family-tree ‘*family’, :controller => ‘people’, :action => ‘show’

This gives you an array called ‘family’ with the whole family tree and
works quite nicely!

DAZ

On May 19, 12:03 pm, “Ryan B. (Radar)” [email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs