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 8126311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T07:06:31+00:00 2026-06-06T07:06:31+00:00

I have a form on a page that a user can use to create

  • 0

I have a form on a page that a user can use to create a course. While I understand how to get provide the user the opportunity to fill in simple things like the course name and description, I also want to assign the user’s id to a teacher_id column. This should be done automatically for the user and they shouldn’t see an opportunity to assign a teacher_id as it should be their id.

Currently the “Create a course” page’s view is this:

<% provide(:title, 'My Classes') %>
<h1>Create a class!</h1>

<div class="row">
   <div class="span6 offset3">
     <%= form_for(@course) do |f| %>

      <%= f.label :name %>
      <%= f.text_field :name %>

      <%= f.label :description %>
      <%= f.text_area :description, :size => "30x6" %>

      <!-- Assign the current user's id to the teacher_id column in the background -->

      <%= f.submit "Create my account", class: "btn btn-large btn-primary" %>
     <% end %>
  </div>
</div>

And the new action in the course controller looks like:

def new
    @course = Course.new
end

What’s the best way to accomplish what I want to do? I’m pretty new to Rails and I haven’t had much experience with forms and all the possibilities. I read a little bit on the hidden_field_tag but I don’t know if that’s the kind of thing I need and I was finding it difficult to get the user’s id in the form anyway. Do I need to make a user instance variable in the controller or can I do it with code in the view somehow.

Thanks for the help.

  • 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-06T07:06:32+00:00Added an answer on June 6, 2026 at 7:06 am

    You can use the hidden_field:

    <% provide(:title, 'My Classes') %>
     <h1>Create a class!</h1>
    
    <div class="row">
       <div class="span6 offset3">
         <%= form_for(@course) do |f| %>
    
          <%= f.label :name %>
          <%= f.text_field :name %>
    
          <%= f.label :description %>
          <%= f.text_area :description, :size => "30x6" %>
    
          <%= f.hidden_field :teacher_id, :value => :current_user.id %>
    
          <%= f.submit "Create my account", class: "btn btn-large btn-primary" %>
         <% end %>
      </div>
    </div>
    

    EDIT – to explain how to have the current_user available in the form.

    I normally have a session controller that is responsible for login and logout users, creating a session to keep the user logged in.

    The create action for example:

      def create
        user = User.find_by_name(params[:name])
        if user && user.authenticate(params[:password])
          session[:user_id] = user.id
          flash[:success] = "Welcome " + current_user.name
          redirect_to user
        else
          flash.now[:error] = "Invalid username/password combination."
          render "new"
        end
      end
    

    If the combination of username and password are correct, it will create a session with the user_id (session[:user_id]).
    Now, in your application_controller you can have something like the below:

    private
    def current_user
      @current_user ||= User.find(session[:user_id]) if session[:user_id]
    end
    helper_method :current_user
    

    The helper_method allows you to use the current_user in any controller or view in your application.

    I hope it helps….

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

Sidebar

Related Questions

I have a user form that is working perfectly. The page makes use of
I have a timed page that I need to use to submit a form
Imagine we have a page with a form where users can create their own
I have been attempting to create a form where a user can simply press
I have a form page in PHP that reads a DBF, and conditionally converts
I currently have a web form aspx page that calls RegisterClientScriptBlock. This sends down
On a page that I'm designing I have a form with one input of
I have a form in an HTM page that, after pressing the submit button,
I have a form on a non-SSL page that I want to submit as
I have a page that have fileupload control, on the submission of the form,

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.