Skip to main content
Ask A Question
You’re not receiving notifications from this thread.

Why is viewer_id 0 on every video?

Rails • Asked by Zanger02

The viewer_id always returns 0 after creating a video. A mentor creates a video with a user via hangouts. Mentor then creates the video on our website. Only the mentor and viewer should see the video. When the mentor creates the video she adds the viewer via a drop down menu listing emails.
In the view I want to see the email for the viewer_id and pass the viewer_id as an integer to the controller.
Not sure what I'm doing wrong.

class CreateVideos < ActiveRecord::Migration[5.1]
  def change
    create_table :videos do |t|
      t.string :title
      t.text :body
      t.integer :user_id
      t.integer :viewer_id
      t.string :url
      t.references :user, foreign_key: true
class VideosController < ApplicationController

 before_action :authorize_user

  def index
    @videos = Video.all

  def new
    @video =
    @users = User.all 


  def create
    @video = Video.create(video_params)
    @video.user = current_user

      flash[:notice] = "Video was created successfully"
      redirect_to video_path(@video)
    else[:alert] = "There was an error saving the video. Please try again."
      render 'new'

  def show
    @video = Video.find(params[:id])

  def edit
    @video = Video.find(params[:id])

  def update
    @video = Video.find(params[:id])

      flash[:notice] = "Video was updated."
      redirect_to videos_path
    else[:alert] = "There was an error saving the video. Please try again."
      render :edit

  def destroy
    @video = Video.find(params[:id])

    if @video.destroy
      flash[:notice] = "\"#{@video.title}\" was deleted successfully."
      redirect_to videos_path
    else[:alert] = "There was an error deleting the video."
      render :show


def video_params
  params.require(:video).permit(:title, :body, :url, :viewer_id)

def authorize_user
   unless current_user.admin? || current_user.mentor? 
     flash[:alert] = "Please sign up as a Premium member"
     redirect_to welcome_index_path


  <div class="container-fluid">
    <div class="row">
      <div class="col-md-8">
     <%= form_for @video do |f| %>
       <div class="form-group">
         <%= f.label :title %>
         <%= f.text_field :title, class: 'form-control', placeholder: "Enter video title", autofocus: true %>
       <div class="form-group">
         <%= f.label :body %>
         <%= f.text_area :body, rows: 4, class: 'form-control', placeholder: "Enter video body" %>
       <div class="form-group">
         <%= f.label :url %>
         <%= f.text_field :url, class: 'form-control', placeholder: "Enter video link" %>
       <div class="form-group">
         <%= f.label :viewer %>
         <%= :viewer_id, options_for_select(, class: 'form-control'  %>
         <!-- a collection of User objects and want to display each user’s email but send user_ids to the controller -->
       <div class="form-group">
         <%= f.submit "Save", class: 'btn btn-success' %>
     <% end %>
 <%= link_to "Back to videos listing", videos_path %>


  id: 9,
  title: "Nathan Alexander",
  body: "Mentor is Robin",
  user_id: 4,
  viewer_id: 0,
  url: "www.something"

Hey Michelle,

At a quick glance, you're setting the viewer_id in your form (which is a database integer column) but your form is submitting email addresses. You probably want to display email address but submit the value as the user's ID instead.

<%= :viewer_id, options_for_select({ |u| [,] })), class: 'form-control'  %>

If you pass in an array for each user of email and id like this example, it should show the email in the form field, but the ID will be the value actually submitted to the server. That should assign the correct value for you then instead of 0.

Thanks Chris!
That fixed the issue returning 0.
I'm still seeing the integer in the view intead of the email.

Viewer: 2

I want the view to show
Viewer: user's email

You'll want to make sure you have your association setup for it

class Video
  belongs_to :viewer, class_name: "User"

And then your view can use that association to load the email:

<%= %>

Thanks! Makes a lot of sense now.
Works great now! Thank you for your quick response.

Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 30,005+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.