How can I make my custom url helpers in rails?
up vote
0
down vote
favorite
I'd like this custom url helper to be accessible from controllers, views, tests... Ending with both _url and _path. Also it should have the possibility to add parameters at the end as a url helper.
def reviews(reviewable)
"/reviews/#{reviewable.class}/#{reviewable.id}"
end
reviews_url Book.find(1) # /reviews/Book/1
reviews_path Film.find(3) # /reviews/Film/3
Review.reviewable is polymorphic.
config/routes
get "reviews/:reviewable_type/:reviewable_id" => "reviews#index"
ruby-on-rails ruby ruby-on-rails-5
add a comment |
up vote
0
down vote
favorite
I'd like this custom url helper to be accessible from controllers, views, tests... Ending with both _url and _path. Also it should have the possibility to add parameters at the end as a url helper.
def reviews(reviewable)
"/reviews/#{reviewable.class}/#{reviewable.id}"
end
reviews_url Book.find(1) # /reviews/Book/1
reviews_path Film.find(3) # /reviews/Film/3
Review.reviewable is polymorphic.
config/routes
get "reviews/:reviewable_type/:reviewable_id" => "reviews#index"
ruby-on-rails ruby ruby-on-rails-5
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'd like this custom url helper to be accessible from controllers, views, tests... Ending with both _url and _path. Also it should have the possibility to add parameters at the end as a url helper.
def reviews(reviewable)
"/reviews/#{reviewable.class}/#{reviewable.id}"
end
reviews_url Book.find(1) # /reviews/Book/1
reviews_path Film.find(3) # /reviews/Film/3
Review.reviewable is polymorphic.
config/routes
get "reviews/:reviewable_type/:reviewable_id" => "reviews#index"
ruby-on-rails ruby ruby-on-rails-5
I'd like this custom url helper to be accessible from controllers, views, tests... Ending with both _url and _path. Also it should have the possibility to add parameters at the end as a url helper.
def reviews(reviewable)
"/reviews/#{reviewable.class}/#{reviewable.id}"
end
reviews_url Book.find(1) # /reviews/Book/1
reviews_path Film.find(3) # /reviews/Film/3
Review.reviewable is polymorphic.
config/routes
get "reviews/:reviewable_type/:reviewable_id" => "reviews#index"
ruby-on-rails ruby ruby-on-rails-5
ruby-on-rails ruby ruby-on-rails-5
asked Nov 21 at 16:30
Chiara Piccinini Núñez
11
11
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
I would just define the routes as:
resources :books do
resources :reviews, only: [:new, :create, :index]
end
resources :films do
resources :reviews, only: [:new, :create, :index]
end
This will create the routes as:
books/:book_id/reviews
films/:film_id/reviews
This is the conventional RESTful way to define nested resources. And more importantly it works with the Rails polymorphic route helpers.
You can use routing concerns to DRY it if needed.
concern :reviewable do
resources :reviews, only: [:new, :create, :index]
end
resources :books, concerns: :reviewable
resources :films, concerns: :reviewable
It gives you the named route helpers book_reviews_path(@book)
and film_reviews_path(@film)
. You can also use the polymorphic route helpers:
<%= link_to "Reviews for #{@reviewable.title}" [@reviewable, :reviews] %>
...
<%= form_for([@reviewable, @reviewable.review.new]) do %>
...
redirect_to [@reviewable, :reviews], notice: 'Review created'
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
I would just define the routes as:
resources :books do
resources :reviews, only: [:new, :create, :index]
end
resources :films do
resources :reviews, only: [:new, :create, :index]
end
This will create the routes as:
books/:book_id/reviews
films/:film_id/reviews
This is the conventional RESTful way to define nested resources. And more importantly it works with the Rails polymorphic route helpers.
You can use routing concerns to DRY it if needed.
concern :reviewable do
resources :reviews, only: [:new, :create, :index]
end
resources :books, concerns: :reviewable
resources :films, concerns: :reviewable
It gives you the named route helpers book_reviews_path(@book)
and film_reviews_path(@film)
. You can also use the polymorphic route helpers:
<%= link_to "Reviews for #{@reviewable.title}" [@reviewable, :reviews] %>
...
<%= form_for([@reviewable, @reviewable.review.new]) do %>
...
redirect_to [@reviewable, :reviews], notice: 'Review created'
add a comment |
up vote
2
down vote
I would just define the routes as:
resources :books do
resources :reviews, only: [:new, :create, :index]
end
resources :films do
resources :reviews, only: [:new, :create, :index]
end
This will create the routes as:
books/:book_id/reviews
films/:film_id/reviews
This is the conventional RESTful way to define nested resources. And more importantly it works with the Rails polymorphic route helpers.
You can use routing concerns to DRY it if needed.
concern :reviewable do
resources :reviews, only: [:new, :create, :index]
end
resources :books, concerns: :reviewable
resources :films, concerns: :reviewable
It gives you the named route helpers book_reviews_path(@book)
and film_reviews_path(@film)
. You can also use the polymorphic route helpers:
<%= link_to "Reviews for #{@reviewable.title}" [@reviewable, :reviews] %>
...
<%= form_for([@reviewable, @reviewable.review.new]) do %>
...
redirect_to [@reviewable, :reviews], notice: 'Review created'
add a comment |
up vote
2
down vote
up vote
2
down vote
I would just define the routes as:
resources :books do
resources :reviews, only: [:new, :create, :index]
end
resources :films do
resources :reviews, only: [:new, :create, :index]
end
This will create the routes as:
books/:book_id/reviews
films/:film_id/reviews
This is the conventional RESTful way to define nested resources. And more importantly it works with the Rails polymorphic route helpers.
You can use routing concerns to DRY it if needed.
concern :reviewable do
resources :reviews, only: [:new, :create, :index]
end
resources :books, concerns: :reviewable
resources :films, concerns: :reviewable
It gives you the named route helpers book_reviews_path(@book)
and film_reviews_path(@film)
. You can also use the polymorphic route helpers:
<%= link_to "Reviews for #{@reviewable.title}" [@reviewable, :reviews] %>
...
<%= form_for([@reviewable, @reviewable.review.new]) do %>
...
redirect_to [@reviewable, :reviews], notice: 'Review created'
I would just define the routes as:
resources :books do
resources :reviews, only: [:new, :create, :index]
end
resources :films do
resources :reviews, only: [:new, :create, :index]
end
This will create the routes as:
books/:book_id/reviews
films/:film_id/reviews
This is the conventional RESTful way to define nested resources. And more importantly it works with the Rails polymorphic route helpers.
You can use routing concerns to DRY it if needed.
concern :reviewable do
resources :reviews, only: [:new, :create, :index]
end
resources :books, concerns: :reviewable
resources :films, concerns: :reviewable
It gives you the named route helpers book_reviews_path(@book)
and film_reviews_path(@film)
. You can also use the polymorphic route helpers:
<%= link_to "Reviews for #{@reviewable.title}" [@reviewable, :reviews] %>
...
<%= form_for([@reviewable, @reviewable.review.new]) do %>
...
redirect_to [@reviewable, :reviews], notice: 'Review created'
answered Nov 21 at 17:13
max
44k856103
44k856103
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53416563%2fhow-can-i-make-my-custom-url-helpers-in-rails%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown