Ask A Question

Notifications

You’re not receiving notifications from this thread.

Why is viewer_id 0 on every video?

Zanger02 asked in Rails

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
      t.timestamps
    end
  end
end
class VideosController < ApplicationController

 before_action :authorize_user

  def index
    @videos = Video.all
  end

  def new
    @video = Video.new
    @users = User.all 

  end

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

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

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

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

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

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

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

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

private

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

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

Videos/new.html.erb

 <section>
  <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>
       <div class="form-group">
         <%= f.label :body %>
         <%= f.text_area :body, rows: 4, class: 'form-control', placeholder: "Enter video body" %>
       </div>
       <div class="form-group">
         <%= f.label :url %>
         <%= f.text_field :url, class: 'form-control', placeholder: "Enter video link" %>
       </div>
       <div class="form-group">
         <%= f.label :viewer %>
         <%= f.select :viewer_id, options_for_select(@users.map(&:email)), class: 'form-control'  %>
         <!-- a collection of User objects and want to display each user’s email but send user_ids to the controller -->
       </div>
       <div class="form-group">
         <%= f.submit "Save", class: 'btn btn-success' %>
       </div>
     <% end %>
   </div>
 </div>
 <%= link_to "Back to videos listing", videos_path %>
 </div>
 </section>

console

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

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.

<%= f.select :viewer_id, options_for_select(@users.map{ |u| [u.email, u.id] })), 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.

Reply

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

Reply

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

class Video
  belongs_to :viewer, class_name: "User"
end

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

<%= @video.viewer.email %>
Reply

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

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 82,464+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.