Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8776699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:07:04+00:00 2026-06-13T19:07:04+00:00

I have a web app where users can sign up, via Devise, and post

  • 0

I have a web app where users can sign up, via Devise, and post pictures for other people to vote on.

I’m trying to attach the users username to the photo that they upload, but I can’t for the life of me figure out how.

I figured out how to get the Devise user_id into the form through a hidden field, but the username just shows up in the database as a blank field (not NULL, just blank).

How do I get the username from Devise to post instead of the user_id?

I have all of the appropriate belongs_to and has_many in the models.

The form.

<% if current_user %>
    <%= simple_form_for Picture.new, :remote => true, :html => { :class => "form-horizontal" } do |f| %>
    <%= f.input :title %>
    <%= f.input :image, :as => :image_preview, :label => false, :input_html => {:preview_version => :thumb} %>
      <div class="control-group"> 
        <div class="controls">
          <a href="#" class="btn" id="file">Choose a File</a>
        </div>
      </div>
      <%= f.input :desc, :as => :text, :input_html => { :cols => 20, :rows => 3 }, :label => 'Description' %>
      <%= f.hidden_field :user_id %>
      <%= f.hidden_field :username %>
      <div class="control-group">
        <div class="controls">
          <div class="btn-group">
            <%= f.submit "Add Picture", :class => 'btn btn-primary' %>
            <button class="btn btn-danger" id="form-cancel">Cancel</button>
        </div>
      </div>
    <% end %>

pictures_controller.rb

def create  
  #This line inserts the user_id into the database
  @picture = current_user.pictures.create!(params[:picture])  
  @picture.save  
    respond_to do |format|  
      format.html { redirect_to pictures_url, notice: 'Thanks for your picture!' }  
      format.js  
    end  
end   
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T19:07:05+00:00Added an answer on June 13, 2026 at 7:07 pm

    You don’t need the hidden fields. Using hidden fields also adds a security risk, a user could upload a picture and make it appear that it was uploaded by another user. To add the current user’s id and username to the picture then all you need to do is modify your controller as follows:

    @picture = Picture.new(params[:picture])
    @picture.user_id = current_user.id
    @picture.username = current_user.username # assuming the user model has a username field
    @picture.save
    

    Although a better way would be to just store the user’s id with the picture and make sure the pictures model has belongs_to :user and the users model has has_many :pictures, rather than storing the username with the picture. Using this method when you want to fetch the username you can access it via picture.user.username

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a local web app that lets users attach and view files. Viewing
So I have a simple web app where I can create a user (UserModel)
I have a simple web app with users and posts. I have an index
I'm currently running a web-app that will send emails to users when they sign-up,
I have an Android app which I use to register users on my web
I have an ASP.NET app that requires users to sign in with their domain
Imagine you're writing a web app that will have 1 million users (they all
We have a C# web app where users will connect using a digital certificate
I have a web-app (let's call it app1),in which I can't even see the
I have an API and a Web app working with Rails and Devise gem

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.