我有一个“问答”部分,如果答案不为 NULL,我想在用户的个人资料 (recipient_id) 上显示它。
前任。
Q: How many letters are in the word 'red'?
A: It has 3 letters.
我不知道如何添加到视图中,以便访问者可以像示例一样查看该用户的 Q&A 部分。
答案控制器:
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} %>
问答模式:
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