我有一個「問題和答案」部分,如果答案不是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
沒有答案,但有一些想法。 1)如果你想看看你的'@ answers'實例變量在視圖中有什麼,你可以使用'<%= debug @answers%>'輸出它的內容yaml格式。 2)你的'AnswersController'可以用'before_action:load_question'來清理,它會爲'AnswersController'上的每個動作(或者只有你想要的)找到並設置'@ question'。 –
我們可以看到「問題」和「答案」表的模式嗎? – kristinalim
@ kristinalim架構已添加。只有一個問題表,沒有答案。 – xps15z