How to Dynamically Call Routes Helper in Rails

How to dynamically call routes helper in rails?

try Rails.application.routes.url_helpers.send(...)

Edit:

As Larry Gebhardt mentioned the url_helpers module is no longer being cached.

Another workaround would be:

cached_helpers = Class.new do
include Rails.application.routes.url_helpers
include Rails.application.routes.mounted_helpers
end.new

cached_helpers.send(...)

Helper function for dynamic routes

Just use [:edit, @contact] instead of the explicit method name.

<li><%= link_to '.... Edit', [:edit, @contact] %></li>

Substitute @contact with whichever variable you wish. I find the array syntax much more readable, and prefer it everywhere. Compare:

 = link_to "Edit", edit_user_post_comment_path(@user, @post, @comment)

vs

 = link_to "Edit", [:edit, @user, @post, @comment]

How can I dynamically call the named route in a :partial in rails?

You can use polymorphic_url. It generates corresponding route based on item types:

Edit: The route is generated based on record's class, so if you pass :event => call or :event => email, it will work like this:

# event.class == Email
polymorphic_url([contact_event, event], :action => :skip)
#=> /contact_events/:contact_event_id/emails/:id/skip

# event.class == Call
polymorphic_url([contact_event, event], :action => :skip)
#=> /contact_events/:contact_event_id/calls/:id/skip

etc.

Edit2:
Routes:

map.resources :contacts do |contact|
contact.with_options :member => {:skip => : ... [get/post - what you have] } do |c|
c.resources :letter
c.resources :emails
c.resources :calls
end
end

dynamically intercept and recall Ruby on Rails Routes in Rails Engine

So I got it to work more or less. I didn't find a solution to this exact problem, so I made small trade-offs. Namely, I decided that the administrator of the host application has to make a small change in the routes.
Basically, I now use a scope like in the following example.

scope path: "/#{EmbedMe.scope_name}", as: EmbedMe.scope_name, is_embedded: true

To make it as easy as possible for the user of the engine, I have integrated a custom route function, which handles the scoping of the routes.

# lib/embed_me/rails/routes.rb
module ActionDispatch::Routing
class Mapper
def embeddable
# note that I call the yield function twice, see describtion below
yield
scope path: "/#{EmbedMe.scope_name}", as: EmbedMe.scope_name, is_embedded: true do
yield
end
end
end
end

# [Host Application]/config/routes.rb
Rails.application.routes.draw do
# not embeddable
get '/private', to: 'application#private'

# is embeddable
embeddable do
get '/embeddable', to: 'application#embeddable'
end
end

# output of $rails routes
...
private GET /private(.:format) application#private
embeddable GET /embeddable(.:format) application#embeddable
embed_embeddable GET /embed/embeddable(.:format) application#embeddable {:is_embedded=>true}
...

Note that I call the yield function twice, once to get the routes normally, and once under the scope. This way I also get multiple path helpers for the normal and the embedded link.

Creating the custom route method is handled similarly to Devise. See Source

View must get dynamically build path from a helper

You can add it to your class helper and call it directly there. For example, if your model call "posts", you can have a file posts_helper.rb inside your helpers folder with something like

module PostsHelper
def your_method
end
end

and in your view,

<li><%= link_to "Classrooms", your_method %></li>

You can also add it to application_helper.rb, but you should do that only if you intend to use it globally.



Related Topics



Leave a reply



Submit