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
Related
I am trying to edit app/views/active_storage/blobs/_blob.html.erb, the partial file created when installing active_storage which allows you to customise how blobs appear inside action_text output.
On other projects, this has worked without issue, on a new Rails 7 project it would appear that the partial added during install is being ignored, as such no edits are coming through.
I can see this by making a small text change (so not adding an HTML element which may get stripped).
For example, I've added "this is a test" in a few places, this does not show:
app/views/active_storage/blobs/_blob.html.erb:
<figure class="attachment attachment--<%= blob.representable? ? "preview" : "file" %> attachment--<%= blob.filename.extension %>">
<% if blob.representable? %>
<%= image_tag blob.representation(resize_to_limit: local_assigns[:in_gallery] ? [ 800, 600 ] : [ 1024, 768 ]) %>
<% end %>
<figcaption class="attachment__caption">
<% if caption = blob.try(:caption) %>
<%= caption %>
this is a test
<% else %>
<span class="attachment__name"><%= blob.filename %></span>
<span>this is a test</span>
<span class="attachment__size"><%= number_to_human_size blob.byte_size %></span>
<% end %>
</figcaption>
</figure>
In other projects, when you view the log output in development it will correctly say "rendered app/views/active_storage/blobs/_blob.html.erb", on this project it doesn't.
File uploads work via actiontext, and rendering the file works by way of the built-in partial, but I can't get the customisable version to be viewed at all.
This is happening on both development and production environments, so I don't think it is anything like a weird eager loading/caching issue.
Does anyone have any ideas as to where I could start looking to debug this further?
rails: 7.0.2.3
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"
I am having an issue with carrierwave/active-record in my rails application. My problem is that the following code resolves all of the images to null
<% Ad.all.limit(30).each do |ad| %>
<img src="<%= ad.carrier_image.url %>" >
<% end %>
Where as the following renders all the images just fine
<% Ad.all.limit(30).each do |ad| %>
<img src="<%= Ad.find(ad.id).carrier_image.url %>" >
<% end %>
The urls are there, just on the initial loop the carrier_image does not seem to be preloaded into the active record objects I think this is an issue with my understanding of rails eager loading, but I am having trouble figuring out how one would avoid this issue
What happens when you use the rails image_tag helper? You're also missing the brackets off the url call as per the carrierwave documentation, but I don't think that's what's causing you drama.
<%= image_tag ad.carrier_image_url() %>
As a side note, you should really move the "Ad.all.limit(30)" out of your view if you wish to keep it all "railsy".
I am following along with the 'Agile Web Development with Rails 4' guide and have come to a section on caching. Here are the steps to follow:
in config/environments/development.rb
config.action_controller.perform_caching = true
In app/models/product.rb
def self.latest
Product.order(:updated_at).last
end
in views/store/index.html.erb
<% cache ['store', Product.latest] do %>
<% #products.each do |product| %>
<% cache ['entry', product] do %>
<div class="entry">
<%= image_tag(product.image_url) %>
<h3><%= product.title %></h3>
<%= sanitize(product.description) %>
<div class="price_line">
<span class="price"><%= number_to_currency(product.price) %></span>
</div>
</div>
<% end %>
<% end %>
<% end %>
To verify the cash is working, the book says: "As far as verifying that this works, unfortunately there isn't much to see. If you go to that page, you should see nothing change, which in fact is the point! The best you can do is to make a change to the template anywhere inside the cache block without updating any product and verifying that you do not see that update because the cached version of the page has not been updated".
However, when I try and add a "Hello" string in the code within the cache blocks, it appears on the page. I've done all the server restarting and what not.
But, when I reload the page on my local host I do see this line
Cache digest for app/views/store/index.html.erb: 6c620ede1d4e824439a7b0e3b177620f
which is not their when I have
config.action_controller.perform_caching = false
Link to git hub repo: https://github.com/BrianLobdell/depot
Thank you,
Brian
Rails updates the cache when changing the file, so it's easier to manipulate the cache instead.
You can retrieve the cache fragment using the cache digest for the page in your Rails console (the digest might have changed, make sure to use the recent one!):
key = ['store', Product.latest, '6c620ede1d4e824439a7b0e3b177620f']
ActionController::Base.new.read_fragment(key)
This should return the cached fragment. To ensure that Ruby actually hits the cache when serving the page, you could replace the fragment with some other content:
ActionController::Base.new.write_fragment(key, 'it works!')
Reload the page, you should see "it works!".
I'm following the 'Agile Web Development with Rails 4' book and I'm stuck at page 105 about the caching.
I have the following code in index.html.erb
<% if notice %>
<p id="notice"><%= notice %></p>
<% end %>
<h1>Your Pragmatic Catalog</h1>
<% cache ['store', Product.latest] do %>
<% #products.each do |product| %>
<% cache ['entry', product] do %>
<div class="entry">
<%= image_tag(product.image_url) %>
<h3><%= product.title %></h3>
<%= sanitize(product.description) %>
<div class="price_line">
<span class="price"><%= number_to_currency(product.price) %></span>
</div>
</div>
<% end %>
<% end %>
<% end %>
The first doubt is the following:
1) What does cache ['store', Product.latest] exactly do? It creates a cache, available from all StoreController actions, named "store" and associates the cache with the Product.latest: why should I do the last thing? Why do I need to associate my cache to a Product.latest?
Always at the same page the book says: "As far as verifying that this works, unfortunately there isn't much to see. If you go to that page, you should see nothing change, which in fact is the point! The best you can do is to make a change to the template anywhere inside the cache block without updating any product and verifying that you do not see that update because the cached version of the page has not been updated".
So I tried something like this:
<% cache ['store', Product.latest] do %>
"hello"
........
........
<% end %>
But I still get this update, the page shows me the "hello" string, why is it so? Shouldn't I see it?
P.S. Obviously I edited my config/environments/development.rb and restarted the server
Start your application on production mode and you will see that caching works.
What did you edited in development.rb? Please compare it to production.rb and copy-paste all caching configuration properties.
I had the same "problem", and I think what the book is saying is not correct. As far as I can tell, the reason why you see the changes on the website, even though caching is active, is that Rails 4 uses cache digests. For more information, check out:
http://blog.remarkablelabs.com/2012/12/russian-doll-caching-cache-digests-rails-4-countdown-to-2013
When you changed config.cache_classes to false, you just told Rails to not automatically regenerate views with each request. That's why you stopped seeing changes, it's not because "caching" was working correctly.