rails route string concat method - ruby-on-rails

I have routes... foo_path, and foo_bar_path. Simple helpers in a link_to.
In the same link, I'm trying to define 2 separate paths...
#foo might be of class Foo, or it might be of class FooBar. It's polymorphic. So, I'd like to build the path with some sort of #foo.class.name.underscore call to prepend to _path.
I can't send it raw with link_to 'foo', "/#{#foo.class.name.underscore}/:id" because I have to send some parameters with it, so only using the route helper works. Unless you know how to send params with that bugger... that'd might simplify things.
Does that make any sense?

Rails will infer the path name from the object type. If you pass a Foo object, it will use foo_path. If you pass a FooBar object, it will use foo_bar_path.
<%= link_to 'foo', foo %>
Assuming foo is one of either a Foo or a FooBar object.
I cover this in-depth in my blog post called "Polymorphic Routes".

Related

How can I get the route from an object array in a model?

I have a model that is responsible for manipulating resource paths. I would like to leverage some Rails routing helpers if I can. I don't want to make any assumptions of the type of resource that is being manipulated, as I would like that to be generic.
http://edgeguides.rubyonrails.org/routing.html#creating-paths-and-urls-from-objects
states that "You can also use url_for with a set of objects, and Rails will automatically determine which route you want:"
<%= link_to 'Ad details', url_for([#magazine, #ad]) %>
From what I've found url_for helpers typically expect a hash that includes the controller, action, etc. What I have been looking for is what the above url_for seems to be returning, which is the resource path from an array of objects. I'm not sure if I'm just using the wrong namespace, but looking through the API docs, I can't find a namespace that would provide the above method.
Just to clarify, I am trying to do something such as below in a model:
resource_array = [parent_resource, child_resource]
resource_path = url_for(resource_array)
I usually keep any routing code out of a model class, but this class is dedicated to manipulating resource paths. Thanks in advance.
Edit:
This is the syntax that I ended up using:
Rails.application.routes.url_helpers.polymorphic_path(resource)
Edit (2):
I feel like there has to be a nicer way to do this, but this is what I have right now.
resource_path = ''
resource_array.each do |resource|
resource_path += '/' + resource.class.name.pluralize.underscore + '/' + resource.id.to_s
end
I'm not sure this is what you're asking for, but url_for is an instance method of the module ActionDispatch::Routing::UrlFor, so you need to extend or include that module to gain access to the method.

can i underscore a url path

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):
send(application_path.to_sym, #application)
From the docs:
send(symbol [, args...]) → obj
Invokes the method identified by symbol, passing it any arguments specified.
But in this case, because your dynamic string is simple enough, Rails has a built-in method to do this, url_for:
url_for(#application)
Example from the docs:
<%= url_for(#workshop) %>
# calls #workshop.to_param which by default returns the id
# => /workshops/5
Note: this is assuming your #application is a model object where the route matches the model name. In your code there seems to be two instance variables, #object and #application, but you haven't explained them fully, so you may need to modify the above to pass in #object instead of #application.
The method url_for does exactly what you're trying to do.
url_for(#application)

How Do I Pass A Parameter From link_to To A Controller?

Building a project with Rails 3.1
I would like to provide a link in a parent model's show page that passes the parent model's id to a child object controller's create method.
I'm guessing this will require a custom route, but have no idea how to build it.
Is this possible? Or would I be breaking a Rails convention? I don't want to use a multiple model form if I can avoid it.
Thanks!
You can just pass them in as arbitrary values:
link_to my_path(:extra_attribute => value, :foo => 'bar')

Adding a route to match a specific pattern

I have a controller in Ruby on Rails and I would like to do the following:
When a user is trying to access the controller I want to match some parameters. For example
domain.com/hello/19213/sayHello
I want to translate this to:
domain.com/hello/:id/:method
in my routes files.
Keep in mind that :method parameter might not exist but :id will always be there.
In addition if it is possible I would like to match the specific :method with a method in my controller. If not I plan to use a switch case.
Can someone provide an example for this and what I have to put in my routes.rb?
I found this:
match ':controller(/:action(/:id(.:format)))'
But I believe that this is somehow different as I only want to match router for the specific controller not every controller in my project.
Thanks
By method, I assume you mean a method you have in your model. If that is the case, I believe you want something like this in your routes.rb
match ':controller/:id(/:method)'
The parentheses around :method mean that it is optional.
If you do not include a method, it will evaluate your SHOW action with params[:id] = :id
If you do include a method, it will evaluate the :method with params[:id] = :id
This is for methods you have defined in your controller

Ruby On Rails Helpers -- Using instance variables in helpers

I have a helper for a controller:
module CourseStepsHelper
def current_quiz_result
#course_step.step.step_quiz.quiz_attempts.where(:patient_id => current_user.id, :step_quiz_id => #course_step.step.step_quiz.id).first
end
end
It has access to #course_step which is defined in the CourseSteps controller show "action". Is this common practice to use instance variables in helpers?
Depending on the level of detail for this quiz result you may actually want to use a partial. In which case the syntax would be:
<%= render :partial => 'quiz/results', :locals => { :quiz => #quiz } %>
If it's relatively simple and you think it should be in a helper you should make simply make quiz a parameter. Requiring views to provide a specific instance variable to use your helper would likely be frowned upon by other developers.
def quiz_result(quiz) # no need to call it "current" when we supply quiz
# do some stuff
end
It also looks to me that you may want to restructure your models in some way. As you can see I implemented my examples with a Quiz class. I'm not sure what your data model looks like but when you are calling properties that are nested that deep it's generally a sign that something is wrong.
I haven't seen a good argument presented for either case, and stumbled onto this question when I was searching for an answer. Personally, I've been using the instance variables in helper methods where it's possible as this is the dryest approach across both helper and view. Rather than passing the instance variable from my view and defining my helper method to accept it, I can just use it directly in the helper. Slightly less typing, anyway...

Resources