# Search form: POST vs GET

**URL:** <https://www.ruby-forum.com/t/search-form-post-vs-get/123222>\
**Category:** Rails\
**Created:** [December 15, 2007, 12:31am UTC](https://www.ruby-forum.com/t/search-form-post-vs-get/123222 "2007-12-15T00:31:05Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![chrisolsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c6cbf5/32.png) [@chrisolsen](https://www.ruby-forum.com/u/chrisolsen)\
**Post date:** [December 15, 2007, 12:31am UTC](https://www.ruby-forum.com/t/search-form-post-vs-get/123222/1 "2007-12-15T00:31:05Z")

</div>

I have a page where members are searched for and added to the group  
specified by the url

## [http://localhost:3000/groups/1/add\_members](http://localhost:3000/groups/1/add_members)

\<%- form\_tag :controller =\> :groups, :action =\> :add\_members do -%\>  
Find Member by Name: \<%= text\_field\_tag “search\_text” %\>  
\<%= submit\_tag “Find” %\>  
\<%- end -%\>

\<%- for member in @members -%\> \<%- end -%\>

| \<%= link\_to "Add to Group", add\_member\_to\_group\_url(@group) %\> | \<%= member.full\_name %\> |

The thing that I am having a hard time with is setting up the routes  
file since both get and post requests are made for the same page.

To access the page the user clicks on a link  
([http://localhost:3000/groups/1/add\_members](http://localhost:3000/groups/1/add_members)) that takes them to a page  
(shown above). The user then searches for a member to add to the group,  
which is where the POST occurs.

The controller code (shown below) should then return the members, but it  
seems that since the url was accessed through a post, the routing has a  
hard time handling it.

def add\_members  
@group = Group.find(params[:id])  
unless params[:search\_text].blank?  
@members = Member.find\_all\_by\_name(params[:search\_text])  
else  
@members = []  
end  
end

When the search is attempted I then get the following message returned:

Unknown action  
No action responded to 1

which makes sense since the routes.rb file only expects GET requests for  
the url:  
map.resources :groups, :has\_many =\> :members, :member =\> {:add\_members  
=\> :get}

and the routes file doesn’t like an additional :add\_members =\> :post (it  
was worth a try)

I have made the form submit via a get request, which does make more  
sense, and it works. Is this the recommended way to do it? It just  
seems that rails is very anti ugly urls, ex:  
[http://localhost:3000/groups/1/add\_members?search\_text=&commit=Find](http://localhost:3000/groups/1/add_members?search_text=&commit=Find)

Thanks for the help.

---

<div class="post-metadata">

**Author:** ![chrisolsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c6cbf5/32.png) [@chrisolsen](https://www.ruby-forum.com/u/chrisolsen)\
**Post date:** [December 18, 2007, 3:47pm UTC](https://www.ruby-forum.com/t/search-form-post-vs-get/123222/2 "2007-12-18T15:47:26Z")

</div>

On 12/14/07, Chris O. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> The thing that I am having a hard time with is setting up the routes
> 
> Unknown action  
> No action responded to 1
> 
> which makes sense since the routes.rb file only expects GET requests for  
> the url:  
> map.resources :groups, :has\_many =\> :members, :member =\> {:add\_members  
> =\> :get}
> 
> and the routes file doesn’t like an additional :add\_members =\> :post (it  
> was worth a try)

You seem to be trying to partially use restful design. I think that  
the “rails way” would be to use a routing like this:

map.resources :groups do | group |  
group.resources :members  
end

And then have a restful controller design for the members controller.

Instead of a special add\_members action you would use the normal rest  
actions

class MembersController \< ApplicationController

before\_filter :load\_group

def load\_group  
@group = Group.find(params[:group\_id])  
end

# GET /groups/1/members/new

# GET /groups/1/members/new.xml

def new  
# get a list of members to present to the user to add to the group.  
# the actual set of members which can be added is determined by  
# business logic in the group model.  
# The form rendered by the new action should set params[:id] to the  
# id of the member selected. It also needs to set params[:group\_id]  
to  
# the id of the group. The best way to do this is via a hidden  
field on the form

```
@members = @group.potential_new_members

respond_to do |format|
  format.html # new.html.erb
  format.xml { render :xml => @member }
end

```

end

# POST /groups/1/members

# POST /groups/1/members.xml

def create

```
respond_to do |format|
  # The group model should have a method to add a member, possibly 

```

with  
# some validition. It should return false/nil if the add operation  
fails  
if @group.add\_member(Member.find(params[:id]))  
flash[:notice] = ‘Member was successfully created.’  
format.html { redirect\_to(group\_member\_path(@group, @member)) }  
format.xml { render :xml =\> @member, :status =\> :created,  
:location =\> @member }  
else  
format.html { render :action =\> “new” }  
format.xml { render :xml =\> @member.errors, :status =\>  
:unprocessable\_entity }  
end  
end  
end  
end

The url to prompt for a new member to be added to group 1 would be

/groups/1/members/new

If you really wanted to allow /groups/1/addmembers you could add an  
additional route.

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)
