I wanted to add some pagination to my rails project.
I've already added Kaminari and I've managed to display only 10 records per page. But I'm already missing the next/prev arrow and the page indicator.
I'm using Kaminari.paginate_array(#array).page(params[:page]).per(10)
This is the only thing I've added until now.
I don't know if it's important, but in my view I have #array.to_json
What should I add to display the arrows?
View code:
<% content_for :create_button do %>
<%= create_button(new_battery_reset_path) %>
<% end %>
<div class="tab-content">
<%= paginate #battery_resets %>
<div class="tab-pane active" id="battery-resets" role="tabpanel"
data-battery-resets='<%= #battery_resets.to_json %>'>
</div>
<div class="tab-pane" id="profile" role="tabpanel">...</div>
<div class="tab-pane" id="messages" role="tabpanel">...</div>
</div>
controller code:
def index
#battery_resets = Kaminari.paginate_array(BatteryResetDecorator.build_collection(
::Filters::BatteryReset.new(
current_account.battery_resets.includes({ device: :account },
:device_inspection)
).apply(permited_params[:q])
)).page(params[:page]).per(10)
respond_with(#battery_resets)
end
You might want to put <%= paginate #array %> in your rails view.
Also try to read gem's wiki first before asking any questions.( kaminari wiki )
Per the docs:
Just call the paginate helper:
<%= paginate #battery_resets %>
This will render several ?page=N pagination links surrounded by an
HTML5 <nav> tag.
This is the same pattern for will_paginate (another gem) also.
--
In regards your fa_icon error, that's caused by the font-awesome-rails gem; it means the helper is not available.
The way to fix it is to make sure you're only using the bundled files with Kaminari. If you've changed _next_page.html.erb in any way, the error is likely coming back now.
--
The quick fix for the fa_icon error is to add font-awesome-rails to your Gemfile:
#Gemfile
gem "font-awesome-rails"
Related
I am building a website for a music database.
I would like to display categories like classical and hip hop first; when the user clicks on a category, I would like to display all the tracks names in that category (I would like to do this without a page refresh). I would also need to support pagination of this list displaying 10 items in one page (and I need to display buttons for display, edit, delete alongside each track in this list)
How can I accomplish this? Does bootstrap provide components that support this kind of implementation?
(If I could do this with a page refresh, I have done this before and I would know how to do it. But I am not sure how to do this without a page refresh)
On views/categories/index.html.erb:
<% #categories.each do |category| %>
<%= link_to category_path(category), :remote => true do %>
<%= category.name %>
<% end %>
<div class="category_<%= category.id %>">
</div>
<% end %>
Then create a partial views/categories/_show.html.erb (here you can insert a table or a structure with bootstrap row and columns):
<% #category.tracks.each do |track| %>
<%= track.name %>
... and you can add here delete and edit actions
<% end %>
Then create views/categories/show.js.erb:
$(".category_<%= #category.id %>").html("<%= escape_javascript(render("show")) %>");
Fyi what you are trying to do has nothing to do with bootstrap, unless you wanted for instance display tracks on a bootstrap modal.
Here is a poor man's bootstrap example I whipped up: when you click on the Track Category example, the table should expand. (You must have bootstrap v4 set up for this to work) and you must have a track_table partial as well - you can use the same partial for the two different tables that you want:
<div class="track-group">
<div class="track track-default">
<div class="track-heading">
<h4 class="track-title">
<p data-toggle="collapse" href="#collapse1"> Track Category (Click to Expand)</p>
</h4>
</div>
<div id="collapse1" class="track-collapse collapse">
<div class="track-body">
<%= render 'track-table' %>
</div>
<div class="track-footer">
</div>
</div>
</div>
</div>
Here's how it looks with an existing app on my computer:
I updated Rails to 5.2 version with ActiveStorage
Everything works fine, but when I perform a loop through my model attached files in order to display them as bootstrap cards, I see my files attributes on the page.
I don't want it to display anything.
How can I prevent that?
<strong style="margin: 10px">Files :</strong>
<div class="card-columns">
<%= #mymodel.files.each do |file| %>
<% end %>
</div>
what it makes on my page
Remove the = from your loop header...
Instead of
<%= #mymodel.files.each do |file| %>
Use
<% #mymodel.files.each do |file| %>
Checkout what the difference is
I understand Kaminari perform well with Rails3 reading this article: Rails 3 pagination, will_paginate vs. Kaminari, but how about with Rails4? Also, when stylizing them with Bootstrap3, which gem is easier solution?
In my experience, there is very little difference between Kaminari & Will Paginate - it's mainly a personal choice as to which you use (rather like Paperclip / Carrierwave or Mac / Windows)
In terms of compatibility, both gems work natively with Rails 4
Bootstrap
In reference to Bootstrap, I think you're asking the wrong question
Bootstrap is a CSS framework, which has no bearing on the backend functionality of your app
Bottom line is you're going to have to call the pagination methods from your controller, and so the differences of the systems will only be cosmetic. If you use Bootstrap to stylize them, you'll have to do the same with either gem
So the choice is yours!
It is pretty easy to implement twitter bootstrap pagination with Kaminari. Just follow the steps below:
Add gem 'kaminari' to your GemFile. Run bundle install and restart rails server
Check the Kaminary themes - in your case you need the bootstrap3 theme
Run rails g kaminari:views bootstrap3
That's it.
Kaminari works fine for me with Rails 4.1.5
You can get it working with Bootstrap 3 by changing one line of code in the generated Bootstrap theme for Kaminari
In Views/Kaminari/_paginator.html.erb
Change this line: <div class="pagination"><ul>
To this: <ul class="pagination pagination-lg">
..and get rid of the div; just use the ul above --works fine for me.
Here is the code for the whole partial:
<%= paginator.render do %>
<ul class="pagination pagination-lg">
<%= first_page_tag unless current_page.first? %>
<%= prev_page_tag unless current_page.first? %>
<% each_page do |page| %>
<% if page.left_outer? || page.right_outer? || page.inside_window? %>
<%= page_tag page %>
<% elsif !page.was_truncated? %>
<%= gap_tag %>
<% end %>
<% end %>
<%= next_page_tag unless current_page.last? %>
<%= last_page_tag unless current_page.last? %>
</ul>
<% end %>
I'm working with an RoR project where I've implemented a search page which is working fine, what I'm looking to achieve is the number of listings.
Actually, what I've used here is hardcoded HTML since I don't know what code or gem to use. Any suggestions how I can achieve this?
Below code snippet might help you:
<div class="digg_pagination">
<div clas="page_info">
<%= page_entries_info #posts %>
</div>
<%= will_paginate #posts, :container => false %>
</div>
See this link for more details.
You can also try a gem called 'will_paginate'.
Here's its homepage on Github.
You need to use pagination through GEM kaminari(replacement of will_paginate). Just see this Railcast.
I have a trouble with kaminari pagination and bootstrap styles.
Pagination works fine, but styles are broken.
Is there any way to fix it? or is there any other style sheets for kaminari pagination?
here is a screenshot http://imageshack.us/photo/my-images/59/kaminari.png/
Browser Google Chrome
= paginate #activities, :theme => 'twitter-bootstrap'
You can also run
rails g kaminari:views bootstrap
This might be due to missing i18n translations -- they create extra tags which messes up bootstrap pagination.
checkout https://github.com/gabetax/twitter-bootstrap-kaminari-views
I've had the same problem and after some experimentation I ended up with this in my gem file;
gem "kaminari", "~> 0.13.0"
gem "kaminari-bootstrap", "~> 0.1.2"
The page links are working the way I think they are supposed to, although I dont like the way they look.
I get an error message if I use Kaminari 0.14.0, so back it off to 0.13.0.
Also...
I noticed it messes things up if I generate Kaminari views. It is the most stable to use the views that come with the gem, if you can live with them.
If you're using Bootstrap 3 you'll need to do the following. In the console:
rails g kaminari:views bootstrap
Then, replace the contents of app/views/kaminari/_pagination.html.erb with the following:
<%= paginator.render do %>
<ul class="pagination">
<%= first_page_tag unless current_page.first? %>
<%= prev_page_tag unless current_page.first? %>
<% each_page do |page| -%>
<% if page.left_outer? || page.right_outer? || page.inside_window? %>
<%= page_tag page %>
<% elsif !page.was_truncated? %>
<%= gap_tag %>
<% end %>
<% end %>
<%= next_page_tag unless current_page.last? %>
<%= last_page_tag unless current_page.last? %>
</ul>
<% end %>
Replace app/views/_first_page.html.erb with the following (note the change to the class of li and from link_to_unless to link_to):
<li <%= "class=disabled" if current_page.first? %>>
<%= link_to raw(t 'views.pagination.first'), url, :remote => remote %>
</li>
Also replace the contents of _prev_page, _last_page, and _next_page in a similar fashion (you'll just need to change "current_page.first?" to "current_page.last?"