Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Theoretical: method_missing

I just started to learn Rails and I cannot understand that:

In my Post controller I do not have method show (not described), but I put in my controller that:

def method_missing(name, *args)
  redirect_to posts_path
end

I think that if controller couldn't find action show - it would call method_missing and after that redirect to index method, but Rails tries to render view show.html.erb.

Why is method missing not catching? How can I use method_missing?

like image 915
Dmytro Vasin Avatar asked May 20 '26 20:05

Dmytro Vasin


1 Answers

Rails does not require action to be present in controller if corresponding template exists. It just assumes empty action and renders template, that is why your method_missing isn't invoked.

If you don't need show action anyway - just remove show.html.erb and method_missing will work as expected.

like image 173
Victor Deryagin Avatar answered May 23 '26 09:05

Victor Deryagin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!