Reputation: 903
I'm trying to link the input of a form to a specific action in my rails app.
Currently if I go to www.myapp.com/check/:idNumber
, I'll be able to trigger the action just fine (which means routes is setup properly?). This action is basically a function call to a ruby/rails script with the parameter "idNumber" being passed to it. If the function is successful, it would return a newly created "Person" object and nil otherwise. This is different than the standard new
operation as it determines the the attributes based on some information that it obtained from a database somewhere else.
Rake routes does give me the following:
check /check/:idNumber(.:format) person#check {:id=>/\d+/}
What I'm having trouble implementing is the form itself.
<%= form_tag("/check", :method => "get") do %>
<%= text_field_tag(:idNumber) %>
<% end %>
Controller action:
def check
regCheck = RegCheck.new
@person = regCheck.check_id(params[:idNumber])
if @person.name == nil
redirect_to root_path
end
end
submitting the form above would bring me to myapp.com/check?utf8=✓&idNumber=1234
instead. Can someone tell me what am I doing wrong?
Upvotes: 3
Views: 6540
Reputation: 6185
I don't think it's possible the way you're trying.. The URL for the form is created before
the user inputs any data.. So you need to remove the :idNumber
from your routing..
If you do you get the following route:
check /check(.:format) person#check
Because the regex is removed now, you need to do this in you're controller:
def check
# Make sure ID is digits only
idNumber = params[:idNumber].gsub(/[^\d]/, '')
regCheck = RegCheck.new
@person = regCheck.check_id(idNumber)
if @person.name == nil
redirect_to root_path
end
end
You're form is allright, but you may want to use check_path
like TheBinaryhood suggests..
If you really want it to be check/:idNumber
you may also be able to submit the form to another action and redirect it to the right path from there..
Upvotes: 0
Reputation: 231
I believe that using the check_path
helper that is generated from the routes file is your best bet.
The form should look like this then.
<%= form_tag(check_path) do %>
<%= text_field_tag(:idNumber) %>
<% end %>
Upvotes: 4
Reputation: 8006
Rails forms can be finicky, especially when trying to build really customized forms.
This line
= form_for [@object]
Determines where the form goes, as well as the object that is being implemented. If you want to route the form to a different place, you can user the :url
option. This options determines the path of the form, however you must keep in mind that the method is determined by the @object
. If it is a new object, the method will be POST, an existing object will use a PUT method.
Let's suppose you want to update an existing object, but you want to send in data for a new object belonging to the existing object. That would look like
= form_for [@object], :as => @child_object, :url => my_optional_custom_path do |f|
# etc...
This generates a form sending a PUT request to the custom url (or the update path for @object
if no custom url is supplied. The PUT request is sent with the parameter params[:child_object]
.
Hopefully this helps!
Best,
-Brian
Upvotes: 2