Nicolas Brousse

Joined

4,150 Experience
39 Lessons Completed
0 Questions Solved

Activity

One cool thing is we also could use layouts. It is possible to create a layout app/views/layouts/application.turbo_stream.erb and manage the code related to flash messages once for all turbo stream response. (Don't forget to use yield 😅)

<%= turbo_stream.prepend :flashes partial: "shared/flashes" %>

<%= yield %>

Posted in Rails 6.0 + Heroku + Active Storage + VIPS

You're welcome :)

About https://github.com/brandoncc/heroku-buildpack-vips/blob/master/bin/compile#L52. It's more because we don't really know the author and what could contain https://bc-heroku-buildpacks.s3.amazonaws.com/vips/libvips-$VIPS_VERSION-$HEROKU_STACK.tar.gz. But that's only my point of view.

On one of our project, hosted on Heroku in production, We only have config.assets.css_compressor = :sass commented. And config.assets.js_compressor = Uglifier.new(harmony: true) is not commented.

Not sure but this may be related to sprockets that is no longuer installed with Rails 6, since webpacker is now the default. (https://github.com/rails/rails/pull/33079)

Posted in Rails 6.0 + Heroku + Active Storage + VIPS

Does https://github.com/brandoncc/heroku-buildpack-vips.git really required?

I'll be not really confident with this line https://github.com/brandoncc/heroku-buildpack-vips/blob/master/bin/compile#L52.

The VIPS part now works with Heroku, but I'm still struggling with some bug. For some reason my CSS and JS files are not loading on Heroku, but they are loading fine locally.

What kind of error do you have? Assets returns 404? All assets?

Does your app is configured with RAILS_ENV=production in environment variables?

Posted in Rails 6.0 + Heroku + Active Storage + VIPS

Posted in I18n access ".title" key with view context in layout

In fact `@virtual_path` is accessible inside `ApplicationHelper`. But it returns the path for the layout.

So finally what I would like, is to know the view name of the view called in the action controller.

Posted in I18n access ".title" key with view context in layout

Hi!

I try to create a small view helper to manage content for `<title>`.

module ApplicationHelper
  def page_title(options = {})
    app_name = Rails.application.class.to_s.split("::").first

    key = "#{controller_name}.#{action_name}.title"
    title = content_for(:page_title) || t(key, default: "")

    [app_name, title].join(" : ")
  end
end

I use high_voltage gem. If I do `t(".title")` it attempt to call `en.pages.about.title` and in my helper it attempt to call `en.pages.show.title`.

I try to found a method to use instead `action_name` to get `about` instead `show` but I didn't found. 
I looked the rails code and found this method is used, but I can't access to the `@virtual_path`.

I think there is a proper way to do this, but I didn't found for now.  
Does somebody has any idea?

Thanks!

Posted in `fresh_when` usage for dynamic queries

I tried and yes if I use a static Time the query stay the so ETag too. That's how I discovered why my ETag was always different.

But anyway if I have a query without something variable like my Time.now it mean that if I update a Post the ETag will stay the same. And that's not logic.

Posted in `fresh_when` usage for dynamic queries

Hi Jacob,

I'm using Rails 5.0.2. Yes, I saw in their doc that they speak about using fresh_when for collections: http://api.rubyonrails.org/classes/ActionController/ConditionalGet.html#method-i-fresh_when.

With the example of your link, it works because Post.all will always generate the same query. And by checking how the Rails code works, I saw they call cache_key method who generated a string key in function of the SQL query.

In my case, because of Time.now, the SQL query is always different. So, the ETag too...

That's why I don't know if it is a problem of usage or a problem onto Rails code.

Posted in `fresh_when` usage for dynamic queries

Hey!

I'm recently looking to use fresh_when method on my PostController.

class PostsController < ApplicationController
  def index
    @posts = Post.published.order(published_at: :desc)
    fresh_when @posts
  end

  def show
    @post = Post.published.find(params[:id])
    fresh_when @post
  end
end
class Post < ApplicationRecord
  scope :published, -> { where("published_at IS NOT NULL AND published_at <= ?", Time.now) }
  scope :unpublished, -> { where("published_at IS NULL OR published_at > ?", Time.now) }
end

All is good for the show method, ETag and Last-Modified headers looks good.

But it's not the case for the index method. Each request the ETag is not the same.

I took a look at Rails code and I discover that the ETag generation is done by generate_strong_etag who call a bit later retrieve_cache_key.
In index action @posts return an ActiveRecord::Relation object who reponds to cache_key method. And this method return a key based on the sql request.
So because of the Time.now usage, all requests are different ...

I resolve the problem by doing this:

fresh_when @posts, etag: @posts.to_a

But I'm not sure if it is the best way to do this.
That's why I asking you here :)

Posted in Looking for Rails work? / Hiring Rails developers?

SEEKING WORK. Located in France. Remote work

I'm Ruby on Rails developer with +5 years of experience in web development.
I've some basics on DevOps.
I use: Ruby, Ruby on Rails, RSpec, PostgreSQL, SCSS, memcached, GitLab (+ GitLab CI), capistrano and auto-deployment, vagrant.

email: nicolas@pantographe.studio
website: http://nicolas-brousse.fr

Posted in Caching static pages without CSRF token

Great!

I'll keep CSRF token for static pages then.

Thanks for your help!

Posted in Caching static pages without CSRF token

Hi Chris,

Thanks for your answer and sorry for the time I spent to send mine.

In my case I'm looking for caching a full-page. My website doesn't contain form for now. It's only static pages. It will have a blog and may be a contact page with a form in the futur.

It's why I was looking to have an cache control as public for main of the pages. I looked https://www.fastly.com/blog/caching-uncacheable-csrf-security, but I plan to use nginx to cache pages. So I can't use ESI. I could use cookie or an endpoint.
But may be in my case, I could just no care about meta tags and cache them anyway. Until I don't use cache control public in page with form.

Posted in Caching static pages without CSRF token

Hi,

I'm looking to use expires_in method in my actions who returns static content.
For this pages I would like to return a cache control header where public is true. Like this it allow an proxy cache to store the content.

My question is more about CSRF token meta tags. I'm looking to have the CSRF meta tags not printed out when I use cache control has public. I was first looking to play with protect_from_forgery options but still print the tags.

I finally saw into Rails code that csrf_meta_tags are only printed if protect_against_forgery? returns true. And that is linked to self.allow_forgery_protection.

So I do this code that works for me.

class ApplicationController < ActionController::Base
  ...

  def expires_in(seconds, options = {})
    super
    self.allow_forgery_protection = false if options.fetch(:public, false)
  end
end

But may be there is a better way to do it or may be I miss some Rails tools.

What do you think about?

Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more.

© 2024 GoRails, LLC. All rights reserved.