Captcha conception

Hi guys, the next problem:

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:

  1. 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?

  1. 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?

  2. When I’ll create all of this, how I can do interaction between other
    pages and captcha?

Thanks, Fred.

You might also want to look at the recaptcha captcha - it’s very easy
to integrate

But I want my own captcha.

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.

  1. So, when I create a methods that can create captcha image and check
    it, what i must do next?

On Sep 9, 10:54am, Misha O. [email protected] wrote:

views/rmagick/show.html.erb:

config/routes.rb:

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

  1. 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

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’

And in _generator.html.erb:

<%= image_tag(captcha_path) %>

So, I have this error, when I open 127.0.0.1:3000/captcha:

ActionController::RoutingError (uninitialized constant Captcha):

Why this error occured?

On Sep 10, 12:35pm, Misha O. [email protected] wrote:

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.

  1. 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

Fred

Thanks, this really works. Can you tell me lesson of rails routing other
than guides.rubyonrails.org? (I cannot understand some things here)

On Sep 12, 1:16pm, Misha O. [email protected] wrote:

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

Fred

So, I have another one problem. How can I do a method in controller,
that the same “.valid_with_captcha?”

When I puts to application_controller.rb

def valid_with_captcha?
if self.valid? and captcha_valid? then true else false end
end

And, supposably, in users_controller.rb

def update
if @user.valid_with_captcha?
save_code
end
end

When I’m updating, I see this:

undefined method `valid_with_captcha?’ for #User:0x00000003f8e6b8

What’s wrong?

bump!