mrg mrg - 1 year ago 73
Ruby Question

How to add named helper for controller in Rails

The following are the routes in my app:

user@localhost:~/HelloWorld$ rake routes
Prefix Verb URI Pattern Controller#Action
biodata_store GET /biodata/store(.:format) biodata#store
greet_welcome GET /greet/welcome(.:format) greet#welcome
greet_wishes GET /greet/wishes(.:format) greet#wishes
time_htime GET /time/htime(.:format) time#htime
login_auth POST /login/auth(.:format) login#auth
POST /biodata/store(.:format) biodata#store
greet GET /greet(.:format) greet#index
time GET /time(.:format) time#index
login GET /login(.:format) login#index
biodata GET /biodata(.:format) biodata#index

In the above routes, I have a controller called 'login'. So when I call 'localhost:3000/login', it provide the 'index.html' file which
is placed in 'app/view/login/' directory. And Once I submit the login form, the entered data goes to '/login/auth' action. Here I
set the method as POST in index.html file. So, I am able to get only '/login' and not '/login/auth' page directly from browser.

Similar to this I create a '/biodata' controller which provide input form to get details from user. And once I submit the form, the
datas are passed to '/biodata/store' using POST method. So, I expect I am not able to access the '/biodata/store' controller directly from
browser. But in my app I am able to access it. How to solve this problem ?

/login --> User can access directly.
/login/auth --> User should not access this page directly.

As mentioned above login controller is properly worked. But the following biodata controller is not properly worked.

/biodata --> User can access directly.
/biodata/store --> User should not access this page directly.(But now it is accessible).

I expect it is due to named helper. Because there is no entry in named helper for the controller /biodata/store. So how to solve this problem?


Rails.application.routes.draw do
get 'biodata/store'
get 'greet/welcome'
get 'greet/wishes'
get 'time/htime'
post 'login/auth'
post 'biodata/store'
get 'greet' => 'greet#index'
get 'time' => 'time#index'
get 'login' => 'login#index'
get 'biodata' => 'biodata#index'

Answer Source

User can access this page directly because you have a

get 'biodata/store'

route in your routes.rb. Just remove that and you will be good to go

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download