Ste*_*lar 7 ruby-on-rails rails-activestorage
我正在使用 ActiveStorage 开发 Rails 6 应用程序。我正在使用 devise 进行身份验证,这需要在 sign_up 页面上提供电子邮件和密码。成功注册后,我想重定向到编辑个人资料。用户不一定需要上传头像或其他一些字段。如果这是用户的选择,这些可以留空。
ActionView::Template::Error (variant delegated to attachment, but attachment is nil):
3:
4: <p><%= @user.full_name %><p>
5: <p>
6: <%= image_tag @user.avatar.variant(resize_to_limit: [100, 100])%>
7: <p>
8: <p><%= @user.city %><p>
9: <p><%= @user.bio %><p>
Run Code Online (Sandbox Code Playgroud)
我收到以下错误,但我希望这些字段是可选的,我该如何实现?用户/edit.html.erb
<%= form_for @user do |f| %>
<div class="form-group">
<%= f.label :avatar %>
<%= f.file_field :avatar, as: :file, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :full_name, 'Full Name' %>
<%= f.text_field :full_name, autofocus: true, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :city, 'City' %>
<%= f.text_field :city, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :bio, 'Bio'%>
<p> Why did you join ArtsySpace?
What should other people here know about you?
</p>
<%= f.text_field :bio, class: "form-control"%>
</div>
<div class="form-group">
<%= f.submit "Edit profile", class: "btn btn-primary" %>
</div>
<% end %>
Run Code Online (Sandbox Code Playgroud)
我有以下表格。如果没有上传头像,我希望不会在show.html.erb页面中抛出 nil 错误。
Eye*_*dic 15
你检查这个 attached?
<% if @user.avatar.attached? %>
<%= image_tag @user.avatar.variant(resize_to_limit: [100, 100])%>
<% end %>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5079 次 |
| 最近记录: |