Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 建立一个问答系统;作为rails中的应用程序,我有点不确定如何向特定用户发送问题_Ruby On Rails_Ruby_Devise_Model - Fatal编程技术网

Ruby on rails 建立一个问答系统;作为rails中的应用程序,我有点不确定如何向特定用户发送问题

Ruby on rails 建立一个问答系统;作为rails中的应用程序,我有点不确定如何向特定用户发送问题,ruby-on-rails,ruby,devise,model,Ruby On Rails,Ruby,Devise,Model,正如标题所示,我正在RubyonRails中构建一个Q&a应用程序(如ask.fm),在将问题发送给特定用户时遇到了一些问题 我有3个模型,一个用户模型(来自Desive),一个具有此属性的问题模型:content:text,以及一个具有此属性的答案模型:content:text 这是他们的模型 class Answer < ActiveRecord::Base belongs_to :question belongs_to :user end class Question &

正如标题所示,我正在RubyonRails中构建一个Q&a应用程序(如ask.fm),在将问题发送给特定用户时遇到了一些问题

我有3个模型,一个用户模型(来自Desive),一个具有此属性的问题模型:content:text,以及一个具有此属性的答案模型:content:text

这是他们的模型

class Answer < ActiveRecord::Base
  belongs_to :question
  belongs_to :user
 end

class Question < ActiveRecord::Base
  has_one :answer
  belongs_to :user
end

 class User < ActiveRecord::Base
   has_many :questions
   has_many :answers
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
    devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable
  end
class-Answer
下面是我创建的迁移

这个问题添加了一个用户id(这样我就可以检查是哪个用户发送了问题,顺便说一句,这个效果很好)

class AddUserIdToQuestion
在这里,我尝试添加一个接收者(将得到问题的用户),但我不工作,当我创建一个问题时,当我在rails控制台中检查它时,它将等于“nil”(检查控制器以查看我做了什么)

类AddReceiverToQuestion 这是问题控制器

  class QuestionsController < ApplicationController

def new
    @question = Question.new
end

def create
    @question = Question.new(question_params)   
    @question.user_id = current_user.id

    if @question.save
        redirect_to root_path
    else
        render 'new'
    end
end



private

def question_params
    params.require(:question).permit(:content, :user_id)
end
end
类问题控制器
我还有一个用户配置文件页面,其中显示了一个表单,这里是该表单的控制器

class UsersController < ApplicationController

def show
  @user = User.find(params[:id])
  @user_questions = @user.questions
  @question = Question.new
end

def create
  @user = User.find(params[:id])

  @question = Question.new(question_params) 
  @question.receiver = @user.id #This does not work 
  if @question.save
      redirect_to root_path
  else
      render 'new'
  end
 end



private

def question_params
    params.require(:question).permit(:content, :user_id, :receiver)
end

 end
class UsersController

我真的希望你们中的一些人知道我能在这里做些什么,谢谢:)

@question.receiver=@user
应该可以试一下。它仍然是零,你知道我是否必须把它保存在某个地方。似乎接收器没有保存任何东西啊,它是一个整数列。应该是一个
接收器id
。然后对
所属对象:receiver,class\u name:'User'
提出疑问。那么我该如何将receiver更改为receiver?我应该执行rake db:rollback吗
  class QuestionsController < ApplicationController

def new
    @question = Question.new
end

def create
    @question = Question.new(question_params)   
    @question.user_id = current_user.id

    if @question.save
        redirect_to root_path
    else
        render 'new'
    end
end



private

def question_params
    params.require(:question).permit(:content, :user_id)
end
end
class UsersController < ApplicationController

def show
  @user = User.find(params[:id])
  @user_questions = @user.questions
  @question = Question.new
end

def create
  @user = User.find(params[:id])

  @question = Question.new(question_params) 
  @question.receiver = @user.id #This does not work 
  if @question.save
      redirect_to root_path
  else
      render 'new'
  end
 end



private

def question_params
    params.require(:question).permit(:content, :user_id, :receiver)
end

 end