ruby on rails will_paginate change default link - ruby-on-rails

In the routes.rb I set lists#index as the root:
root to: "lists#index"
I would like will_paginate to create page hrefs pointing to /?page=# instead of /lists?page=#. To create the paginate links I have the following line in the "lists#index" file:
<%= will_paginate #lists %>

The solution was indeed a custom renderer. Thanks Ayush!
The following helped me to solve it: how-to-customize-the-will-paginate-links-with-images
I created a file: app/helpers/will_paginate_helper.rb with the following content:
module WillPaginateHelper
class RootedLinkRenderer < WillPaginate::ActionView::LinkRenderer
protected
def link(text, target, attributes = {})
if target.is_a? Fixnum
attributes[:rel] = ""
target = "/?page=#{target}"
end
attributes[:href] = target
tag(:a, text, attributes)
end
end
end
Finally, I updated the index file and replaced:
<%= will_paginate #lists %>
with:
<%= will_paginate #lists, renderer: WillPaginateHelper::RootedLinkRenderer %>
So the href is http://localhost:3000/?page=2 and not http://localhost:3000/lists?page=2.

What I understand from your question that you are trying to create a custom renderer, although I have never used it, but for that you need to override the link method of renderer.
here is the link to the original code might be of some help -
https://github.com/voormedia/paginary/blob/master/lib/paginary/helpers/pagination_helper.rb

In this particular case the links are influenced by the order of the routing instructions in the routes.rb file.
If you place the line
root to: "lists#index"
at the top of routes.rb then will_paginate will generate links to /?page=# without the need for a custom renderer.
This can be found in the FAQs for the module in coursera.

Related

How to put query params on _path rails_helper

The problem is to pass Query params throuth the helper _path .
Example I know how to pass a normal param
edit_survey_path(#poll_module)
which generates
/survey/12 because of route /survey/:id
Which is right and okay for editing
However, I want to generate the following url for new_survey_path
/survey/new?pollModule=lgpd
so how should I write the
new_survey_path(????)
At the view's controller I have the #poll_module = 'lgpd'
Then I trided
new_survey_path(#poll_module)
new_survey_path({#poll_module})
new_survey_path({pollModule: #poll_module})
new_survey_path({:pollModule => #poll_module)
new_survey_path(:pollModule, {pollModule: #poll_module})
yes some are crazy, but it were things I have found on foruns
new_survey_path(#poll_module) generates
http://rubyenv:3000/surveys/new.lgpd?
that's because the route for edit is
#routes
new_survey GET /surveys/new(.:format) so is gets on the format
I want a query param
/surveys/new?pollModule=lgpd
because on the new survey I need to search the righ questions before answering the survey, and this question depende on the module
The problem it was not the _path helper, but the componet I was using on the view
if you use the button_to, it has already a parameter for parameters
{poll_module: 'teste123'} %>
doc ref ->
https://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-button_to
or if i you use the link_to it get the query by default
doc -ref->
https://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to
Normally you pass a query param like this new_survey_path({key: "value1", key2: "value2"}). So the one version you have reported that you tried should have worked. Thats why i assume it could interfere with your routes. Can you try to isolate the problem and simply add the following to your project:
# routes.rb
resources :surveys, only: [:new]
# app/controllers/surveys_controller.rb
class SurveysController < ApplicationController
def new
#pollModule = params[:pollModule]
end
end
<!-- app/views/surveys/new.html.erb -->
<h1>New Survey</h1>
<p>poll module inserted as query param: <%= #pollModule %></p>
<%= link_to "testLink", new_survey_path(pollModule: "randomInput") %>
When you click on the "testLink" it should redirect you to the URL localhost:3000/surveys/new?pollModule=randomInput.
Also be sure that the route is at the top of your routes.rb file, since sometimes the ordering is what messes things up. Hope that helpes and let me know if it worked.

Rails will_paginate - how to make nicer route?

I have on the homepage list of articles that are listed with using will_paginate gem.
Everything works well, but there is one thing I would want to improve - on the homepage, the data are loaded into home controller and index action.
So, when I load my website - www.website.com, data are loaded into home/index. When I click on the pagination link for the second page, the link is www.website.com/home/index?page=2. I would like to see there in the best way www.website.com/page/2 (or www.website.com/?page=2).
Basically, the point is to remove from the URL /home/index - is there any way to do this?
Thanks
You may do it this way - add this class to some helper module, for example app/helpers/application_helper.rb:
module ApplicationHelper
class SmartLinkRenderer < WillPaginate::ActionView::LinkRenderer
protected
def link(text, target, attributes = {})
if target.is_a? Fixnum
attributes[:rel] = rel_value(target)
target = url(target)
end
attributes[:href] = target.gsub(/[?&]page=(\d+)/,'/page/\1')
tag(:a, text, attributes)
end
end
end
You may customize it according to your needs, for example do
attributes[:href] = target.gsub(/home\/index/,'')
or whatever. And then you may do this in your views:
<%= will_paginate #items, :renderer => 'ApplicationHelper::SmartLinkRenderer' %>
Try this
root to: 'home#index'
in you config/routes.rb file
EDIT
to be able to route these kind of requests:
www.website.com/page/2
add this to routes.rb:
match "page/:page" => "your_root_controller#index"
this way :page will be in your params hash.
and for example index method to view your Message model can be:
def index
#messages = Messages.paginate(page: params[:page])
end
hope that can help. for more information please refer to RoR routing

Acts as taggable on Rails

How can I make an index route/action to see all the tags that I've created!, I'm using the gem at https://github.com/mbleigh/acts-as-taggable-on
Thanks.
Use ActsAsTaggableOn::Tag.all to list all the tags, for example
# tags_controller.rb
def index
#tags = ActsAsTaggableOn::Tag.all
end
# tags/index.html.erb
<% #tags.each do |tag| %>
some html to display
<% end %>
Of course if you don't have a tags controller you will have to create one, and the routes in routes.rb. You can use the rails generate controller tags index command if you want. Run rake routes to see what the routes are.
You can take a look at the actual Tag model here: https://github.com/mbleigh/acts-as-taggable-on/blob/master/lib/acts_as_taggable_on/tag.rb
And the fields available for Tag you can find in your db/schema.rb file.

How to add a link back to the application in ActiveAdmin?

I need to add a few links to certain pages of the application in the ActiveAdmin pages. I can do this using sidebars, but I'll have to repeat the code for each of my resources. Is there anyway of adding custom links to the header ? Or define a sidebar that will appear for all resources ?
I also wouldn't want to overlook setting config.site_title_link in initializers/active_admin.rb.
I'm pretty sure it takes a symbol representing the name of a route from your application, for example:
config.site_title_link = :root
would link the site title to your application's root_path.
Thanks #phoet ! Implemented it by overriding the HeaderRenderer instead:
module ActiveAdmin
module Views
class HeaderRenderer
def to_html
title + global_navigation + application_link + utility_navigation
end
def application_link
link_to('Back to Application', root_url)
end
end
end
end
i think there is no build-in way to do it, but you can override the render-logic in the TabsRenderer (2.2) / TabbedNavigation (3.0):
def render_menu(menu)
content_tag :ul, :id => #options[:id] do
menu.items.collect do |item|
render_item(item)
end.join.<<('your_custom_stuff').html_safe
end
end

Template path in Rails 3

Let's say, I connected the route / to WelcomeController's index action.
Inside of the index.html.erb-Template I want to display the path of the template from Rails.root upwards, ie.
<h1> We are rendering: <%= how_do_i_do_this? %></h1>
to render to
<h1> We are rendering: app/views/presentation/index.html.erb</h1>
In Rails 2 I could access template.path, but this doesn't work anymore
Any ideas?
Because of how template rendering works in Rails, you will now be able to use __FILE__ for this instead. This works for me:
<%= __FILE__.gsub(Rails.root.to_s, "") %>
There may be a better way to do this however, but I couldn't find it when I went looking.
Ryan's answer works. If you also want to put your method in a helper, use Kernel#caller. Here is a method I'm using to do something similar:
def has_page_comment? code = nil
if code.nil?
# grab caller file, sanitize
code = caller.first.split(':').first.gsub(Rails.root.to_s,'').gsub('.html.erb','')
end
...
end

Resources