0

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

3
  • Short answer is yes, you can do this, and it doesn't look very different no matter what controller you're calling it from. There is no firm rule that you must only create records of a single type in a controller. I can make an answer, but first, a question - are you using paperclip to handle your uploads? Or some other gem? Commented May 24, 2019 at 22:46
  • I'd suggest using ActiveStorage unless you have a reason not to. Commented May 25, 2019 at 5:34
  • @maxpleaner no, I use mini-magick Commented May 25, 2019 at 13:20

2 Answers 2

1
# new action
def new 
  @user = User.new
  @user.pictures.build
end

# create action
def create
  @user = User.new(user_params)
  @user.save
end

private
def user_params
  params.require(:user).permit(:name, pictures_attributes: [:id]) 
# in addition to the id you need to specify the other required fields.
end
# User model
class User
  has_many :pictures
  accepts_nested_attributes_for :pictures, allow_destroy: true
end
# form view

<%= form_for @user do |f| %>
  pictures:
    <%= f.fields_for :pictures do |picture_form| %>
    # your code

Sometimes in f.fields_for :pictures, you need to write :pictures_attributes by hand, if the first option does not work for you:

<%= f.fields_for :pictures_attributes do |picture_form| %>

and write field names by hands:

<%= f.fields_for :pictures_attributes do |picture_form| %>
  <%= picture_form.text_field :name, name: "user[pictures_attributes][][name]" %>
Sign up to request clarification or add additional context in comments.

Comments

0

I think what you are looking for is nested forms. Nested forms accepts params of a child class also and in the view you can add as many images as you want and it will get stored in its respective table. Check out - https://github.com/ryanb/nested_form

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.