-1

I have this action in the controller families

 def hello
 end 

With view hello.html.erb

 hello world 

In the view of index in the same controller i have

<%= link_to 'hello',  families_hello_path %>

In my routes.erb i make this

post  'families/hello', to: 'families#hello' 

but when un click on hello i have this error :

Couldn't find Family with 'id'=hello 

where is the problem ?

Abhishek kumar
  • 4,347
  • 8
  • 29
  • 44
massi
  • 11
  • 1
  • 6

3 Answers3

1

Couldn't find Family with 'id'=hello

Definitely the request isn't going to hello method. As you declared the route as post, you should use method: :post in the link_to to send the request to hello method

<%= link_to 'hello', families_hello_path, method: :post %>
Pavan
  • 33,316
  • 7
  • 50
  • 76
0

Here rails thinks that you have a route like /families/:id and you are doing a GET to the url of /families/hello. So rails thinks that you mean that 'hello' is the id of a Family record and goes to the show action.

If you go to your terminal and run rake routes |grep families you will see all of the routes you have configured for families and be able to adjust till you get the correct one. You should also take note of the http method which tells you POST for your current configuration in which case you have to use

<%= link_to "hello", families_hello_path, method: :post %>

but if you aren't changing data in your :hello action the right solution would be go change your method in your config/routes.rb file to read

get '/families/hello', to: 'families#hello' # Note changing 'post' to 'get' in the front.
Tyrone Wilson
  • 4,328
  • 2
  • 31
  • 35
0

You have two options to fix this: The first one is to change the route.rb file

get 'families/hello', to: 'families#hello' 

The second one is to change the view of index

<%= link_to 'hello',  families_hello_path, method: :post %>

I prefer the first option because it's more logical to set the method to a GET request to show a hello world page, but the two options still work.

Ahmed Magdy
  • 191
  • 4
  • Can you provide us with the whole route.rb file? – Ahmed Magdy Aug 23 '17 at 09:46
  • get 'welcome/index' resources :purchases resources :customers resources :sales resources :products resources :users root 'welcome#index' post 'sales/add_produit', to: 'sales#add_produit' post 'purchases/add_produit', to: 'purchases#add_produit' get 'contacts/:id/del_role', to: 'contacts#del_role' get 'families/hello', to: 'families#hello' post 'contacts/add_role', to: 'contacts#add_role' get 'sales/:id/delete_produit/:product_id' , to: 'sales#delete_produit' get 'purchases/:id/delete_produit/:product_id', to: 'purchases#delete_produit' – massi Aug 23 '17 at 09:52
  • I think I found your problem which is that you have something like `before_action :set_family` in your controller which will lead for this problem, if so you can fix it with `before_action :set_family, except: [:hello]`, I hope this help you. – Ahmed Magdy Aug 23 '17 at 10:49
  • Thank you for your answer but i did it before, i did this and is work "<%= link_to 'hello', {action: 'hello'}, {method: :post} %>" but only if i click on hello...and if i want to click on hello and open another window i will have the same error !!! i think that the problem in the resources ?? – massi Aug 23 '17 at 12:07