小编sle*_*n36的帖子

Rails ActiveStorage错误 - MessageVerifier-InvalidSignature

我正在开发一个需要模型ActiveStorage has_many_attached :photos情况的项目Location.

我在下面设置了代码,但在尝试上传表单时,收到以下错误:

ActiveSupport::MessageVerifier::InvalidSignature in 
                                 LocationsController#attach_photo
Run Code Online (Sandbox Code Playgroud)

这是将文件"添加"到特定父记录(即:Location记录)的附件集的方法吗?

Location 模型

class Location < ApplicationRecord
  ...
  has_many_attached :photos
  ...
end
Run Code Online (Sandbox Code Playgroud)

地点控制器

class LocationsController < ApplicationController
  ...
  def attach_photo
    @location = Location.find(params[:id])
    @location.photos.attach(params[:photo])
    redirect_to location_path(@location)
  end
  ...
end
Run Code Online (Sandbox Code Playgroud)

视图

<%= form_tag attach_photo_location_path(@location) do %>
  <%= label_tag :photo %>
  <%= file_field_tag :photo %>

  <%= submit_tag "Upload" %>
<% end %>
Run Code Online (Sandbox Code Playgroud)

视图

resources :locations do
  member do
    post :attach_photo
  end
end
Run Code Online (Sandbox Code Playgroud)

ruby ruby-on-rails rails-activestorage

6
推荐指数
2
解决办法
3279
查看次数

设计:无法验证服务器上启用HTTPS的CSRF令牌真实性(无JSON/API)

我正在建立一个Rails博客.我正在使用Rails 5和Devise 4.2.0.该应用程序使用Nginx和Puma在Ubuntu Server上运行,并与Capistrano一起部署.在Nginx上启用HTTPS(使用有效的SSL证书)并在HTTP中添加301重定向格式之前,所有内容都非常适用于生产.

我检查了生产日志,日志中的真实性密钥与我在浏览器中看到的密钥不匹配.

这是我正在使用的nginx.conf文件:

upstream puma {
  server unix:///home/deploy/apps/example-blog/shared/tmp/sockets/example-blog-puma.sock;
}

server { # Redirect HTTP to HTTPS
  # Bind port(s)
  listen   80;
  listen   [::]:80;

  # Bind domain(s)
  server_name blog.example.com;

  # 301 redirect to HTTPS
  return 301 https://$server_name$request_uri;
}

server { # Primary server block
  # Bind port(s)
  listen   443 default_server ssl;

  # Bind domain(s)
  server_name blog.example.com;

  # Bind certificate(s)
  ssl_certificate       /etc/nginx/ssl/blog.example.com/ssl-bundle.crt;
  ssl_certificate_key   /etc/nginx/ssl/blog.example.com/blog.example.com.key;

  root /home/deploy/apps/example-blog/current/public;
  access_log /home/deploy/apps/example-blog/current/log/nginx.access.log;
  error_log /home/deploy/apps/example-blog/current/log/nginx.error.log info;

  location ^~ /assets/ {
    gzip_static on;
    expires max; …
Run Code Online (Sandbox Code Playgroud)

ssl ruby-on-rails nginx devise csrf-protection

3
推荐指数
1
解决办法
1278
查看次数

查找子资源的总附件存储使用情况 - Paperclip Rails

我的建筑内,所有型号的Rails应用程序Users,Album(如相册)和Photo嵌套的顺序.我希望能够找到用户相册使用的总存储空间.

Album关卡中,我可以找到该相册中照片使用的总存储空间.即:

album = Album.first

album.photos.sum(:image_file_size)
Run Code Online (Sandbox Code Playgroud)

但是,我希望能够从User所有专辑的照片水平上做到这一点.

这样做有一种优雅的方式吗?

谢谢!

sum ruby-on-rails paperclip nested-resources rails-activerecord

0
推荐指数
1
解决办法
39
查看次数

有条件地添加空格+字符串 - Rails 5

我编写了一个快速name模型方法,根据存在的字段返回用户名.

class User < ActiveRecord::Base
  validates :first_name,
    presence: true,
    length: {
      maximum: 64,
    }

  validates :last_name,
    length: {
      maximum: 64,
    }

  def name
    "#{self.first_name}#{" #{self.last_name}" if self.last_name.present?}"
  end
end
Run Code Online (Sandbox Code Playgroud)

如果仅first_name存在该字段,则应返回:

=> "Joe"
Run Code Online (Sandbox Code Playgroud)

如果两者first_namelast_name字段都存在,那么它应该返回:

=> "Joe Bloggs"
Run Code Online (Sandbox Code Playgroud)

上面的代码工作,但我觉得它可以清理很多.

这是一种更优雅的方式吗?

谢谢

ruby activerecord ruby-on-rails

0
推荐指数
1
解决办法
256
查看次数