2009-12-04 180 views
0

我對ruby和一般編程極其新穎。在複製,粘貼和祈禱階段,我喜歡稱它。我試圖限制編輯帖子和評論的訪問權限給創作者,但是當我創建帖子時user_id沒有填充到數據庫中。Ruby on rails的關係

在此先感謝您的幫助。

路線

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 

回答

0

您需要範圍模型對相關對象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將@answer.question = @question添加到你的控制器動作中。不要手動設置外鍵,例如當ActiveRecord很樂意爲你做時。

0

你有一個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 
+0

您可以將前兩行合併爲一個:'current_user.answers.new(params [:answer])' – 2009-12-04 23:02:28

+0

感謝您的快速反應。我主要關注問題部分。我在create方法中添加了current_user.questions.new(params [:question]),但是我在nil.save – cachesking 2009-12-05 06:01:00

+0

上得到了「沒有預期它的零對象」沒關係,我知道了。 Andy的線是什麼導致了nil.save問題。即時通訊使用@question之前我初始化它。感謝男性。 – cachesking 2009-12-05 06:12:42