我正在尝试创建一个邀请系统,其中只有收到邀请者的用户才能注册。注册的用户可以为他们的朋友生成邀请代码。每个邀请都应属于用户。
这是控制器:
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
用户模型:
has_many :invites
在索引中:
<% 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 %>
在路线中:
resources :invites
resources :users do
resources :invites
end
以下是我遇到的错误:
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 >
当我
rails generate migration add_user_id_to_invites
答案 0 :(得分:1)
尝试:
rails g migration AddUserRefToInvites user:references
生成user_id列和相应的索引:
class AddUserRefToInvites < ActiveRecord::Migration[5.0]
def change
add_reference :invites, :user, index: true, foreign_key: true
end
end