I create a controller, (/controllers/rmagick_controller.rb), which have
aim to test an captcha
Actions:
def download # creates an captcha image
def show # show the page where image took place
def check # must check right captcha or wrong
views/rmagick/show.html.erb:
<%= image_tag download_rmagick_path %>
<% form_tag check_rmagick_path do %>
<%= text_field_tag :captcha %>
<%= submit_tag "Submit" %>
<% end %>
config/routes.rb:
resources :rmagick do
get “download”, :on => :member
get “check”, :on => :member
end
So, there are 2 problems:
Routes problem. If I’m write in config/routes.rb something like this:
get “rmagick/show”
get “rmagick/download”
get “rmagick/check”
I catch this output:
No route matches {:action=>“download”, :controller=>“rmagick”}
But I’m place get “rmagick/download” in routes.rb. Why it doesn’t work?
(it can’t find download_rmagick_path)
So, if I use resources :rmagick do … I see an show.html.erb when i
write something other than “show” text, ie in this link
127.0.0.1:3000/rmagick/jjvniuv I also will see show action.why? How I
can change this? Where is the problem?
Problem with form_tag. So, you see the code above. When I put captcha
value and press “submit” button, I don’t go to rmagick/check, I go to
rmagick/show/check. Why?
When I’ll create all of this, how I can do interaction between other
pages and captcha?
You’d probably need to either switch
to a singleton resource, make those actions collection actions, use
match rather than creating a resource.
Can you explain example, what I must to do? Guide “Rails routing from
outside in” not gived clarity to me.
So, when I create a methods that can create captcha image and check
it, what i must do next?
resources :rmagick do
get “download”, :on => :member
get “check”, :on => :member
end
Rails understand this as creating urls like rmagick/123/download, i.e.
it things there are actual rmagick entities which supports a download
action
This means that when you call download_rmagick_path rails is expecting
you to supply it with the id of the rmagick entity. If you don’t rails
won’t be able to generate a url. You’d probably need to either switch
to a singleton resource, make those actions collection actions, use
match rather than creating a resource. The rails routing guide should
explain the difference between these actions
But I’m place get “rmagick/download” in routes.rb. Why it doesn’t work?
(it can’t find download_rmagick_path)
So, if I use resources :rmagick do … I see an show.html.erb when i
write something other than “show” text, ie in this link
127.0.0.1:3000/rmagick/jjvniuv I also will see show action.why? How I
can change this? Where is the problem?
That’s what it’s supposed to do - given the routes you’ve defined, /
rmagick/jjvniuv is the path for the rmagick entity with the id jjvniuv
Problem with form_tag. So, you see the code above. When I put captcha
value and press “submit” button, I don’t go to rmagick/check, I go to
rmagick/show/check. Why?
I’m surprised it does anything at all. You’ve got the same problem as
earlier - a route that is expecting an id, but you’re not giving it an
id.
You might also want to look at the recaptcha captcha - it’s very easy
to integrate
Fred, now I’m> You’d probably need to either switch
to a singleton resource, make those actions collection actions, use
match rather than creating a resource.
Can you explain example, what I must to do? Guide “Rails routing from
outside in” not gived clarity to me.
So, when I create a methods that can create captcha image and check
it, what i must do next?
I think you need to think about this bit first - the right routes will
depend on how this is going to be used. For example, I don’t think you
need a check action for the captcha - the user’s answer to the captcha
would be submitted along with the other information they’re supplying
and verification done in the controller. The only captcha specific
action I think you need is the one that actually produces the image.
The form probably also needs to contain enough information to allow
you to know which captcha the user is solving. You’ll probably also
want to ensure that the same captcha/answer can’t be used by someone
over and over again.
Even if you want to write your own, you might find existing captcha
systems a useful source of inspiration.
Fred, I understand a simple_captcha conception. Now I faced with this
problem:
I create an captcha_controller.rb, when in def generate… end I put a
code to create captcha image.
Then I write in config/routes.rb:
match ‘captcha’ => ‘captcha/generate’
You probably want ‘captcha#generate’ rather than ‘captcha/generate’ -
with what you’ve got I think rails will look for
Captcha::GenerateController instead of routing to the generate action
of CaptchaController