Malek Zalfana Malek Zalfana - 5 months ago 14
Ruby Question

Why isn't my invitation system working?

I'm trying to create an invitation system where only users who received who an invite can sign up. Users who sign up can generate an invite code for their friends. Each invite should belong to a user.

Here's the controller:

class InvitesController < ApplicationController
def new
@invite = current_user.invites.new
end
def create
@user = current_user
@invite = @user.invites.create
@invite.code = rand(1000..9000)
end
end


The user model:

has_many :invites


In the index:

<% if user_signed_in? %>
<%= form_for @invite, remote: true do |f| %>
<%= f.hidden_field :user_id, :value => current_user.id %>
<%= f.submit 'post', class: '', name: 'post' %>
<% end %>
<% end %>


In the routes:

resources :invites
resources :users do
resources :invites
end


Here is the error I'm getting:

NameError: undefined local variable or method `add_user_id_to_invites' for main:Object
from (irb):6
from /usr/local/rvm/gems/ruby-2.3.0/gems/railties-4.2.5/lib/rails/commands/console.rb:110:in `start'
from /usr/local/rvm/gems/ruby-2.3.0/gems/railties-4.2.5/lib/rails/commands/console.rb:9:in `start'
from /usr/local/rvm/gems/ruby-2.3.0/gems/railties-4.2.5/lib/rails/commands/commands_tasks.rb:68:in `console'
from /usr/local/rvm/gems/ruby-2.3.0/gems/railties-4.2.5/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from /usr/local/rvm/gems/ruby-2.3.0/gems/railties-4.2.5/lib/rails/commands.rb:17:in `<top (required)>'
from /usr/local/rvm/gems/ruby-2.3.0/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:274:in `require'
from /usr/local/rvm/gems/ruby-2.3.0/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:274:in `block in require'
from /usr/local/rvm/gems/ruby-2.3.0/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:240:in `load_dependency'
from /usr/local/rvm/gems/ruby-2.3.0/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:274:in `require'
from /home/ubuntu/workspace/bin/rails:9:in `<top (required)>'
from /usr/local/rvm/gems/ruby-2.3.0/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:268:in `load'
from /usr/local/rvm/gems/ruby-2.3.0/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:268:in `block in load'
from /usr/local/rvm/gems/ruby-2.3.0/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:240:in `load_dependency'
from /usr/local/rvm/gems/ruby-2.3.0/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:268:in `load'
from /usr/local/rvm/gems/ruby-2.3.0/gems/spring-1.7.0/lib/spring/commands/rails.rb:6:in `call'
from /usr/local/rvm/gems/ruby-2.3.0/gems/spring-1.7.0/lib/spring/command_wrapper.rb:38:in `call'
from /usr/local/rvm/gems/ruby-2.3.0/gems/spring-1.7.0/lib/spring/application.rb:191:in `block in serve'
from /usr/local/rvm/gems/ruby-2.3.0/gems/spring-1.7.0/lib/spring/application.rb:161:in `fork'
from /usr/local/rvm/gems/ruby-2.3.0/gems/spring-1.7.0/lib/spring/application.rb:161:in `serve'
from /usr/local/rvm/gems/ruby-2.3.0/gems/spring-1.7.0/lib/spring/application.rb:131:in `block in run'
from /usr/local/rvm/gems/ruby-2.3.0/gems/spring-1.7.0/lib/spring/application.rb:125:in `loop'
from /usr/local/rvm/gems/ruby-2.3.0/gems/spring-1.7.0/lib/spring/application.rb:125:in `run'
from /usr/local/rvm/gems/ruby-2.3.0/gems/spring-1.7.0/lib/spring/application/boot.rb:19:in `<top (required)>'
from /usr/local/rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /usr/local/rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from -e:1:in `<main>'2.3.0 :007 >


when I

rails generate migration add_user_id_to_invites

Answer

Try:

rails g migration AddUserRefToInvites user:references

Which generates a user_id column and appropriate index:

class AddUserRefToInvites < ActiveRecord::Migration[5.0]
  def change
    add_reference :invites, :user, index: true, foreign_key: true
  end
end
Comments