用户表
和
图片表
它的主要工作是存储图片路径、名称和用户id(这样做是因为我想让我的应用程序的用户上传不止一张图片,如果我在users表中为图片创建一列,我将不允许我这样做)。
现在在表单(注册表单)中,如果用户决定上传图片,我希望图片保存在pictures表中,同时仍在
用户控制器创建方法
这就是我到目前为止所做的(新建.html.erb)
<% provide(:title, 'Sign up') %>
<h1>Sign up</h1>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_for(@user, url: signup_path) do |f| %>
<%= render'shared/error_messages', object: f.object %>
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control' %>
<%= f.label :email %>
<%= f.email_field :email, class: 'form-control' %>
<%= f.label :username %>
<%= f.text_field :username, class: 'form-control' %>
<%= f.label :password %>
<%= f.password_field :password, class: 'form-control' %>
<%= f.label :password_confirmation, "Confirmation" %>
<%= f.password_field :password_confirmation, class: 'form-control' %>
<span class="picture">
<%= f.file_field :picture, accept: 'image/jpeg,image/gif,image/png' %>
</span>
<%= f.submit "Create an account", class: "btn btn-primary" %>
<% end %>
</div>
</div>
1. def create
2. @user = User.new(user_params) # Not the final implementation!
3.
4. if @user.save
5. # Save to the picture name to the picture table
6. if params[:picture].present?
7. # How do I save the picture to the picture table from users controller
8. @user.send_activation_email
9. flash[:info] = "Please check your email to activate your account."
10. redirect_to root_url
11. else
12. #@states = State.all
13. render 'new'
14. end
15. end
如果你看6号线和7号线,我有这些
if params[:picture].present?
# How do I save the picture to the picture table from users controller
这就是现在的问题所在,如何将图片名称插入user-controller中的数据库