John_Pear
(John Pear)
December 12, 2009, 8:22pm
1
Hi,
I’m trying to emulate a post from a HTML form. I’ve the following code in erb:
Alterar Password
<% form_tag :action=>“reset_password”, :method=>“put” do%>
<%=label_tag :password, “Nova password” %>
<%=password_field_tag :password, @password %>
<%=label_tag :password_confirmation, “Confirmação Password” %>
<%=password_field_tag :password_confirmation, @password_confirmation %>
<%=submit_tag “Alterar Password” %>
<%end %>
In my action I have something similar to this:
def reset_password
if request.get?
#do something for get
elsif request.put?
#do something for put
else
raise Webapp::BadRequestError
end
rescue Webapp::BadRequestError
logger.error("Invalid Request type. Client IP: "+request.remote_ip)
flash_error(:invalid_request)
redirect_to root_url
end
I’ve functional tests for put and get, and everything is fine. But when the form is rendered and tested in a browser I can’t get this working. In Firefox, the output for the erb is something like:
Nova password
Confirmação Password
Everytime I submit the form I then hit in the Webapp::BadRequestError
Can someone help me get over this?
John_Pear
(John Pear)
December 13, 2009, 1:48am
2
I made some researche and found in Ruby on Rais guides that I’m doing the correct thing… but I’m not getting the correct results. I know I’m missing something… what is?
http://guides.rubyonrails.org/form_helpers.html#how-do-forms-with-put-or-delete-methods-work
Can you help?
Jeffrey
(Jeffrey)
December 13, 2009, 3:57am
3
Try ':method => :put' (without the quotes). Rails is not always consistent.
Some places either strings or symbols work, other places they don't.
HTH,
Jeffrey
Quoting Jonhy Pear <jonhy.pear@gmail.com>:
AD13
(AD)
December 13, 2009, 5:01am
4
John_Pear
(John Pear)
December 13, 2009, 4:12pm
5
Thank you. I tried just that now. But still produce the same result:
And no hidden field named _method, as stated in documentation.
Ok, thank you. My form is not AJAXified. I managed to fix the problem. It probably has something to do with the rendering of the form using form_tag.
If I have hardcoded the hidden field, everything is fine:
<%=password_field_tag :password_confirmation, @password_confirmation %>
This is a hack and I would like to know if you now something about this.
Why don’t you create a controller for ‘reset_password’, using RESTfull methods. Use ‘new’ action for the GET and ‘update’ for PUT.