2013-10-17 97 views
2

我有一個「問題和答案」部分,如果答案不是NULL,我想在用戶的個人資料(recipient_id)上顯示它。如何在視圖中顯示錶格列的數據

Ex。

Q: How many letters are in the word 'red'? 
A: It has 3 letters. 

我不知道如何添加到視圖,讓參觀者可以查看用戶Q值&喜歡的例子一節。

答案控制器:

def new 
    @question = Question.find(params[:question_id]) 
end 

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

def create 
    @question = Question.find(params[:question_id]) 
    if @question.update_attributes(params[:question]) 
    redirect_to questions_path 
    else 
    render :new 
    end 
end 

問題控制器:

def index 
    @questions = Question.all 
    respond_with(@questions) 
end 

def show 
    @question = Question.find(params[:id]) 
    @questions = Question.order("created_at DESC") 
    respond_with(@questions) 
end 

def new 
    @question = Question.new 
    respond_with(@question) 
end 

def create 
    @question = Question.new(params[:question]) 
    if @question.save 
    @message = Message.create(:subject => "You have a question from #{@question.sender_id}", 
     :sender_id => @question.sender_id, 
     :recipient_id => @question.recipient_id, 
     :body => @question.question) 

    @question.message = @message 
    @question.save 
    redirect_to questions_path, notice: 'Your question was saved successfully. Thanks!' 
    else 
    render :new, alert: 'Sorry. There was a problem saving your question.' 
    end 
end 

def update 
    @question = Question.find(params[:id]) 
    @question.update_attributes(:answer => params[:question][:answer]) 
    redirect_to user_messages_path(current_user, :mailbox => "inbox") 
end 
end 

用戶配置文件視圖:

<h5>SHOW QUESTIONS & ANSWERS</H5> 
<%= render :partial => "answers/show", :locals => { :question => @question} %> 

架構對於Q &答:

create_table "questions", force: true do |t| 
    t.string "question" 
    t.string "answer" 
    t.datetime "created_at", null: false 
    t.datetime "updated_at", null: false 
    t.integer "sender_id" 
    t.integer "recipient_id" 
    t.integer "message_id" 
end 

問題型號:

attr_accessible :answer, :question, :sender_id, :recipient_id 

    belongs_to :sender, 
     :class_name => 'User', 
     :foreign_key => 'sender_id' 
     belongs_to :recipient, 
     :class_name => 'User', 
     :foreign_key => 'recipient_id' 

     belongs_to :message 

end 

用戶控制器:

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

def new 
    @user = User.new 
end 

def profile 
    @profile = User.profile 
end 

def create 
    @user = User.new(user_params) 
    if @user.save 
    UserMailer.registration_confirmation(@user).deliver 
    session[:user_id] = @user.id 
    redirect_to root_url, notice: "Thank you for signing up!" 
    else 
    render "new" 
    end 
end 

def show 
    @user = User.find(params[:id]) 
    @question = User.find(params[:recipient_id]) 
    @letsgos = @user.letsgos.paginate(page: params[:page]) 
    @letsgo = current_user.letsgos.build 
end 

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

def index 
    @users = User.all 
end 

def destroy 
    User.find(id_params).destroy 
    flash[:success] = "User deleted." 
    redirect_to users_url 
end 

def update 
    @user = if current_user.has_role?(:admin) 
      User.find(params[:id]) 
      else 
      current_user 
      end 
    @user.update_attributes(params[:user]) 
    respond_with @user 
end 

private 

def user_params 
    params.require(:user).permit(:name, :email, :username, :password, :ethnicity, :gender, :zip_code, :birthday, :role, :age, :sexuality) 
end 
+0

沒有答案,但有一些想法。 1)如果你想看看你的'@ answers'實例變量在視圖中有什麼,你可以使用'<%= debug @answers%>'輸出它的內容yaml格式。 2)你的'AnswersController'可以用'before_action:load_question'來清理,它會爲'AnswersController'上的每個動作(或者只有你想要的)找到並設置'@ question'。 –

+0

我們可以看到「問題」和「答案」表的模式嗎? – kristinalim

+0

@ kristinalim架構已添加。只有一個問題表,沒有答案。 – xps15z

回答

2

好像要表明,用戶Q &基於所提供的recipient_id。如果是這樣的話試試這個...

對於用戶控制器替換@question:

@question = Question.where(recipient_id: params[:id]) 

在你看來要顯示在用戶配置文件將q &答:

<% @question.select(&:answer?).each do |question| %> 
Question: <%= question.question %><br> 
Answer: <%= question.answer %><br><br> 
<% end %> 

這將加載,如果用戶沒有提供答案,它不會顯示在他們的頁面上。希望能幫助到你。

0

首先,我想你可以用在不同型號的一些ActiveRecord associations解決您的問題,讓你拉問題&答案了特定用戶:

#app/models/User.rb 
has_many :questions, :class_name => 'User' 
has_many :answers, :class_name => 'Answer', :through => :question #-> probably won't work right 

#app/models/Question.rb 
belongs_to :user 
has_one :answer 

#app/models/Answer.rb 
belongs_to :user 
has_one :question 

我需要調整這些關聯蒸發散,讓他們正常工作(這既是因爲你的問題有點模糊,而你沒有張貼任何型號代碼),但我希望這會給你的,你可以做些什麼的想法:

#app/controllers/users_controller.rb 
def index #the page you're using to display the questions & answers 
    @user = User.find(params[:recipient_id]) 
end 

#app/views/users/index.html.erb 
<%= @user.questions.each do |question| %> 
    <% if defined?(question.answer) %> 
     <%= question.title %> 
     <%= question.answer.body %> 
    <% end %> 
<% end %> 

看來你正在混合你的控制器&的意見,當真的,我只會專注於users控制器,並通過模型獲取所有數據漏斗。 Rails慣例鼓勵「fat model, skinny controller」,這意味着你應該只使用控制器來ping不同的車型,並讓他們處理邏輯

+0

我從來沒有添加模式,因爲我在Question模型中看起來並不相關。我會發布我的內容。沒有答案模型。我有它的設置,以便用戶答案張貼在自己的列的問題表內。 – xps15z

+0

隨着代碼我得到一個'找不到沒有id'錯誤的用戶。 – xps15z

1

它只是可以使用ActiveRecordAssociation解決您的編碼結構的問題和複雜性。

#app/models/Question.rb 
belongs_to :user 
has_many :answer 

#app/models/Answer.rb 
belongs_to :user 
has_one :question 

#app/models/user.rb 
has_many :questions 
has_many :answers 


#controller/users_controller.rb 
def show 
    @user = User.find(params[:id]) 
    if @user.answers 
    @questions = current_user.answers.collect{|x|x.question}   
    end 
end 

#app/view/user/1/show like ur '**user's profile page**' 
<% if @questions.present?%> 
    <%= @questions.each do |question| %>  
    <%= question.title %> 
    <%= question.answer.body %> 
    <% end %> 
<% end %> 

我希望它會爲你工作任何查詢分享它。 「CURRENT_USER」是一個輔助方法,使用「會議

+0

爲每個問題有多個(包括錯誤的答案)答案,所以,我用關係船**問題has_many答案** – thiyaram

+0

我沒有答案模型。所以我不確定你爲什麼要編寫基於我的代碼。我對任何混淆抱歉。我有我的模式發佈所以'question.title'應該只是'question.question'和'question.answer.body'應該是'question.answer'。我沒有標題或正文欄。問題的「標題」將在「問題」欄下。問題的「主體」將在「答案」欄下。做出這些改變,我得到一個'未定義的方法'問題''這是爲def show'if @ user.question' – xps15z

1

有很多的事情,我不明白爲什麼你在你的代碼做的,所以你可能需要修改,使之適合你的目的,但這是我認爲你想要的。

我假設您的「用戶個人資料頁面」是您通過轉到如/users/profile/:id這樣的路線並且該路線轉到您的UsersController#profile操作的頁面。如果這不是你所擁有的,你需要修改你的路線來使用我的代碼。

那麼你UsersController

def profile 
    # First get the user whose profile you are trying to show 
    # If you do not get an :id that is the User's ID in `params`, you will need 
    # to change your routes. 
    @user = User.find(params[:id]) 
    # Now get the question that has this user as its recipient_id 
    @question = Question.where(recipient_id: params[:id]).first 
end 

現在你的用戶的個人資料查看你提到上面我假設是app/views/users/profile.html.erb可以使用@question像你有它:

<h5>SHOW QUESTIONS & ANSWERS</H5> 
<%= render :partial => "answers/show", :locals => { :question => @question} %> 

再次,如果這段代碼不起作用,那麼很可能是我對你的應用程序設置做了不正確的假設,所以你需要了解Rails的作用以及與你正在嘗試做什麼有關的方式。閱讀您收到的任何錯誤消息並修復他們告訴您的錯誤。

+0

謝謝你試圖提供幫助。我瞭解def配置文件,但這不起作用。我已經嘗試過這裏的例子,創建了我自己的代碼,並在這裏調整了示例,沒有任何工作不幸。我有一個問答功能。用戶可以被另一個用戶詢問問題。問題將顯示在問題下的問題表中。當用戶回答時,他們的回答將出現在「答案」下。該表記錄了「sender_id」和「recipient_id」。參數應該是收集當前的user_id和recipient_id(例如:用戶78配置文件將顯示具有78的recipient_id的問答)。 – xps15z

+0

我忘了提及頁面上沒有產生錯誤。問答部分保持空白,因爲它以前做。 – xps15z

+0

我建議查看日誌以查看正在執行的查詢,並查看他們爲什麼不從數據庫中返回您期望的數據。 – carols10cents

相關問題