gpt4 book ai didi

mysql - 如何在 View 中显示表列中的数据

转载 作者:行者123 更新时间:2023-11-29 01:41:43 24 4
gpt4 key购买 nike

我有一个“问答”部分,如果答案不为 NULL,我想在用户的个人资料 (recipient_id) 上显示它。

例如

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

我不知道如何添加到 View ,以便访问者可以像示例一样查看该用户的问答部分。

答案 Controller :

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

问题 Controller :

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

用户个人资料 View :

<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

用户 Controller :

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

最佳答案

您似乎想根据提供的 recipient_id 显示用户问答。如果是这样,试试这个...

对于用户 Controller ,将@question 替换为:

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

在您想要显示用户个人资料问答的 View 中:

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

这将加载,以便如果用户没有提供答案,它将不会显示在他们的页面上。希望对您有所帮助。

关于mysql - 如何在 View 中显示表列中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19433792/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com