I have this tables which names are
Users table
and
Pictures Table
whose primary job is to store the picture path and name and user id(Am doing it this way because I want to allow the users of my app to upload more than a picture which if I create a column for the picture in the users table will not allow me to do that).
Now inside the form(Signup form), if a user decide to upload a picture, I want the picture to be save inside the pictures table while still in the
Users_controller create method
This is what I have done so far (new.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>
and inside the users controller, I have this in the create method
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
If you look at line 6 and 7, I have these
if params[:picture].present?
# How do I save the picture to the picture table from users controller
That is where the problem is now, how do I insert the picture name into the database inside user_controller