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
<%- form_tag :controller => :groups, :action => :add_members do -%>
Find Member by Name: <%= text_field_tag “search_text” %>
<%= submit_tag “Find” %>
<%- 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) 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
Thanks for the help.