Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rails 7.1 attached image raises error because "Cannot get a signed_id for a new record" in image_tag #50234

Open
gregorbg opened this issue Dec 1, 2023 · 5 comments

Comments

@gregorbg
Copy link

gregorbg commented Dec 1, 2023

Steps to reproduce

Say I have a model with an attached image, like such:

class Company < ApplicationRecord
  has_one_attached :logo
  
  validates :name, presence: true
end

...and there is also a controller to update it using the route defined by resources :companies like such:

def update
  @company = Company.find(params[:id])
  company_params = params.require(:company).permit(:name, :logo)

  if @company.update(company_params)
    flash[:success] = "Successfully updated Company!"
    redirect_to edit_company_path(@company)
  else
    render :edit
  end
end

The edit form is an HTML form that looks as follows:

<%= simple_form_for @company, url: edit_company_path(@company) do |f| %>
  <%= f.input :name %>
  <%= f.input :logo %>
  <% if @company.logo.attached? %>
    <%= image_tag @company.logo.variant(resize: "200x200") %>
  <% end %>
<% end %>

Now when I pass in an update payload that generates an invalid state (as per my Company model's validations) but has a valid, existing, readable logo file, the following error occurs:

ActionView::Template::Error: Cannot get a signed_id for a new record

Failure/Error: <%= image_tag @company.logo.variant(resize: "200x200") %>

     ActionView::Template::Error:
       Cannot get a signed_id for a new record

This is because the @company.update(...) statement in the controller writes the attributes, but the actual raw saving to the database fails because of validations.

The edit form then tries to display the new, updated but not persisted logo in the frontend, which raises the error about signed_id.

Expected behavior

The edit form should render, with the form showing errors on the name field according to the validators of the company model. Uploading a correct, readable logo should not cause the form to throw an error when rendering.

Actual behavior

The error above is being thrown.

System configuration

Rails version: 7.1.2

Ruby version: 3.2.2

@strobilomyces
Copy link

I can't speak to whether the above behavior makes sense, but I handle displaying images in my edit forms like so:

<% if @company.logo.attached? &&
      @company.logo.attachment.blob.present? &&
      @company.logo.attachment.blob.persisted? %>
  <%= image_tag @company.logo.variant(resize: "200x200") %>
<% end %>

@vipulnsward
Copy link
Member

I tried this simple repro but without simple_form- https://github.com/vipulnsward/ActiveStorageTest50234

  • One difference there is no "resize", its "resize_to_limit"

@gregorbg does this happen on a normal form tag for you?

@marckohlbrugge
Copy link
Contributor

marckohlbrugge commented Feb 2, 2024

Running into the similar issue, following https://edgeguides.rubyonrails.org/active_storage_overview.html#form-validation and using regular form helpers

Update: Fixed it for my case. I thought I was using direct-uploads, but I forgot to include the necessary javascript. After I added it, signed_in worked for new records as well. /cc @gregorbg

@reeganviljoen
Copy link

I have recently run into this issue when using active_storage urls when using url_for(file)

@reeganviljoen
Copy link

reeganviljoen commented Apr 25, 2024

@marckohlbrugge the work around is usefull for most situations but but this example in the docs does not seem to work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants