【发布时间】:2019-11-06 15:00:23
【问题描述】:
@user.file.attach(params[:file]): 行出现此错误:
ActiveRecord::RecordNotSaved 在 UsersController#runFile 中。 未能保存新关联的 file_attachment。
我正在使用活动存储来存储文件。
在我的用户模型中,我有这个代码:
has_one_attached :file
在我的用户控制器中, 我有这个代码:
def runFile
```
@user.file.attach(params[:file])
```
end
```
def user_params
params.require(:user).permit(:file)
end
在我的视图中我有这个代码:
<%= form_with(model: user, local: true) do |form| %>
<% if user.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(user.errors.count, "error") %> prohibited
this user from being saved:</h2>
<ul>
<% user.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= form.label :file %>
<%= form.file_field :file %>
</div>
<br>
<h6 class="actions">
<%= form.submit "Create" %>
</h6>
<% end %>
这是我为每个用户单击运行文件的 runFile 方法的显示表单:
<p id="notice"><%= notice %></p>
<%= link_to "Run File", runFile_path, method: :post %>
<%= link_to 'Edit', edit_user_path(@user) %> |
<%= link_to 'Back', users_path %>
【问题讨论】:
-
我在runFile方法中也有@user.save
标签: ruby-on-rails ruby model-view-controller rubygems rails-activestorage