views:

40

answers:

2

Hi All, I am just learning Rails. I had encountered a routing error, though I think I have specified the correct rules in the routing.rb. I have attached the code. Please help

routes.rb

  map.connect ':controller/:action'
  map.connect ':controller/:action/:id'
  map.connect ':controller/:action/:id.:format'

Controller

class EntriesController < ApplicationController
  def sign_in
    @name = params[:visitor_name]
  end
end

View

<html> 
<head><title>Hello <%=h @name %></title></head>
<body> 
<%=h @name %>
<% form_tag :action => 'sign_in' do %>
<p>Enter your name:
<%= text_field_tag 'visitor_name', @name %></p>
<%= submit_tag 'Sign in' %>
<% end %>
</body> 
</html>

Thanks

+1  A: 

Your form_tag needs to specify the controller as well as the action. So:

<%= form_tag :controller => 'entries', :action => 'sign_in' do %>

Note that in Rails' terms, this is a very old-fashioned way of doing things and I'd recommend that you learn about RESTful routes within the Rails Routing from the Outside In guide.

John Topley
Hi. I added the controller name but still it is not working :(
Felix
What is your view template file called and whereabouts is it?
John Topley
Hi John...It worked :) Thanks for your suggestion about RESTful routes :)
Felix
@Steve Glad you got it working in the end!
John Topley
A: 

You have to include follwing in your routes.rb

map.resources :entries, :collection=>{:sign_in=>:post}

Restart your server

<% form_tag :controller=>'entries' , :action => 'sign_in' do %>

if your view in entries folder only then you need not to specify controller name it will by default take it. just write

<% form_tag :action => 'sign_in' do %>
Salil
He doesn't _have_ to include that at all. It's clear from his routes.rb file that he's not using RESTful routing.
John Topley
Thanks for the answer but It is still not working. I added map.resources to the routing.rb and it is the only view in my entries folder.
Felix