I want to dynamically set the url path of a
- application_path = @object.class.name.underscore + "_path"
= link_to "<input type='button' value='Cancel' class ='bigbutton go_back'/>".html_safe, application_path(@application)
but i keep getting
undefined method `application_path' for #<#<Class:0x00000103d11d80>:0x00000103d04a68>
any ideas on how to achieve this behavior
In the general case, you can use Object#send to call a method based on a symbol (which you can get from a string using
to_sym):From the docs:
But in this case, because your dynamic string is simple enough, Rails has a built-in method to do this, url_for:
Example from the docs:
Note: this is assuming your
@applicationis a model object where the route matches the model name. In your code there seems to be two instance variables,@objectand@application, but you haven’t explained them fully, so you may need to modify the above to pass in@objectinstead of@application.