Ruby on Rails 关系

发布于 2024-08-13 17:33:29 字数 3968 浏览 2 评论 0原文

我对 Ruby 和一般编程非常陌生。在我喜欢称之为的复制、粘贴和祈祷阶段。我试图限制创建者编辑帖子和评论的访问权限,但是当我创建帖子时,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

I am extremely new to ruby and programming in general. In the copy, paste, and pray stage as I like to call it. I am trying to restrict access of editing posts and comments to the creator but when i create a post the user_id isn't populating in the database.

thanks in advance for the help.

routes

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"

the user model

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

the question model

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

the answer model

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

enter code here

the question controller

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

answer controller

  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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

羁拥 2024-08-20 17:33:29

您需要将模型的范围限定为 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.buildcurrent_user.answers.find(params[:id ]

由于答案属于用户和问题,因此您必须将范围设置为最有意义的对象,假设您确定它是用户对象,则必须自己设置 Question_id 添加<。 code>@answer.question = @question 到您的控制器操作中,不要手动设置外键,例如 @answer.question_id = @question.id 当 ActiveRecord 很乐意这样做时。为你。

You need to scope your model to the related object for ActiveRecord to populate foreign keys. This is easiest using a helper method. If you wanted to scope to the user:

Excerpted from one of my apps using 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

Then you can scope e.g. current_user.answers.build, or current_user.answers.find(params[:id]

As answers belong to users and questions. You're going to have to set the scope to whichever object makes the most sense. Assuming you decided it's the user object, you'll have to set the question_id yourself Add @answer.question = @question to your controller action. Don't get into setting foreign keys manually e.g. @answer.question_id = @question.id when ActiveRecord will happily do it for you.

神也荒唐 2024-08-20 17:33:29

您是否有经过身份验证的 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

Do you have a current_user that is authenticated? If not, you need one. I haven't used AuthLogic but there should be some good tutorials on how to do that.

Assuming you do have a current_user, the easiest solution would be to do something like:

  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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文