【问题标题】:Ruby on rails relationshipsRuby on Rails 关系
【发布时间】:2009-12-04 21:23:41
【问题描述】:

总的来说,我对 ruby​​ 和编程非常陌生。在我喜欢称之为复制、粘贴和祈祷的阶段。我正在尝试将编辑帖子和 cmets 的访问权限限制为创建者,但是当我创建帖子时,user_id 没有填充到数据库中。

提前感谢您的帮助。

路线

map.resources :user_sessions
map.resources :users
map.resources :questions, :has_one => :user, :has_many => :answers
map.login "login", :controller => "user_sessions", :action => "new"
map.logout "logout", :controller => "user_sessions", :action => "destroy"

用户模型

   class User < ActiveRecord::Base
   acts_as_authentic
   has_many :questions
   has_many :answers
   end

问题模型

class Question < ActiveRecord::Base
  validates_presence_of :question, :tag
  validates_length_of :question, :minimum => 5
  validates_length_of :tag, :minimum =>4
  belongs_to :user
  has_many :answers

end

答案模型

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

enter code here

问题控制器

class QuestionsController < ApplicationController
  before_filter :find_question,
    :only => [:show, :edit, :update, :destroy]
  # GET /questions
  # GET /questions.xml
  def index
    @questions = Question.all

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @questions }
    end
  end

  # GET /questions/1
  # GET /questions/1.xml
  def show

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @question }
    end
  end

  # GET /questions/new
  # GET /questions/new.xml
  def new
    #@question = Question.new
    @user = Question.new
  end

  # GET /questions/1/edit
  def edit

  end

  # POST /questions
  # POST /questions.xml
  def create
    @question = Question.new(params[:question])
    #@question = Question.user.new(params[:question])
      if @question.save
        flash[:notice] = 'Question was successfully created.'
        redirect_to(@question) 
      else
        render :action => "new"
      end
    end
  end

  # PUT /questions/1
  # PUT /questions/1.xml
  def update
      if @question.update_attributes(params[:question])
        flash[:notice] = 'Question was successfully updated.'
        redirect_to(@question)
      else
        render :action => "edit"
      end
  end

  # DELETE /questions/1
  # DELETE /questions/1.xml
  def destroy
    @question.destroy
    redirect_to(questions_url)
  end

  private
    def find_question
      @question = Question.find(params[:id])
    end

应答控制器

  class AnswersController < ApplicationController
  def index
    @question = Question.find(params[:question_id])
    @answer = @question.answers
  end

  def show
    @question = Question.find(params[:question_id])
    @answer = @question.answers.find(params[:id])
  end

  def new
     @question = Question.find(params[:question_id])
     #@question = Question
     @answer = @question.answers.build
     #@answer = Answer.new
     #redirect_to questions_url(@answer.question_id)
  end

  def create
     #@question = Question.find(params[:question_id])
    # @question = Question
   @answer = Answer.new(params[:answer])

    if @answer.save
      redirect_to question_url(@answer.question_id)
    else
      render :action => "new"
    end
  end

  def edit
    @question = Question.find(params[:question_id])
    @answer = @question.answers.find(params[:id])
  end

  def update
    @question = Question.find(params[:question_id])
    @answer = Answer.find(params[:id])
    if @answer.update_attributes(params[:answer])
      redirect_to question_answer_url(@question, @answer)
    else
      render :action => "edit"
    end
  end

  def destroy
    @question = Question.find(params[:question_id])
    @answer = Answer.find(params[:id])
    @answer.destroy

    respond_to do |format|
      format.html {redirect_to @question}
      format.xml {head :ok}
    end
  end

end

【问题讨论】:

    标签: ruby-on-rails netbeans6.7


    【解决方案1】:

    您需要将模型范围限定为相关对象,以便 ActiveRecord 填充外键。这是使用辅助方法最简单的方法。如果您想将范围限定为用户:

    摘自我使用 Authlogic 的应用之一:

    class ApplicationController < ActionController::Base
    
      helper_method :current_user_session, :current_user
    
      protected
    
      def current_user_session
        @current_user_session ||= UserSession.find
      end
    
      def current_user
        @current_user ||= current_user_session && current_user_session.user
      end
    
    end
    

    然后你可以范围例如current_user.answers.build,或current_user.answers.find(params[:id]

    因为答案属于用户和问题。您将不得不将范围设置为最有意义的任何对象。假设您决定它是用户对象,您必须自己设置 question_id 将 @answer.question = @question 添加到您的控制器操作中。不要手动设置外键,例如@answer.question_id = @question.id ActiveRecord 会很乐意为您完成。

    【讨论】:

      【解决方案2】:

      您是否有经过身份验证的current_user?如果没有,你需要一个。我没有使用过 AuthLogic,但应该有一些关于如何做到这一点的好教程。

      假设您有 current_user,最简单的解决方案是:

        def create
         @answer = Answer.new(params[:answer])
         @answer.user_id = current_user.id   <--- add this line
      
          if @answer.save
            redirect_to question_url(@answer.question_id)
          else
            render :action => "new"
          end
        end
      

      【讨论】:

      • 您可以将前两行合二为一:current_user.answers.new(params[:answer])
      • 感谢小伙伴们的快速反应。我开始关注问题部分。我在 create 方法中添加了 current_user.questions.new(params[:question]) 但我在 nil.save 上得到了“你没想到的 nil 对象”
      • 没关系,我明白了。安迪,你的台词是导致 nil.save 问题的原因。我在初始化之前使用@question。谢谢绅士。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-12
      相关资源
      最近更新 更多