如何在视图中显示表列中的数据

时间:2013-10-17 17:52:56

标签: mysql ruby-on-rails

我有一个“问答”部分,如果答案不是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} %>

Q&amp; A的架构:

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

4 个答案:

答案 0 :(得分:2)

您似乎希望根据提供的recipient_id显示用户Q&amp; A.如果是这种情况,试试这个......

对于Users控制器,将@question替换为:

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

在您的视图中,您希望在用户个人资料中显示问答:

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

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

答案 1 :(得分: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 '是帮助方法,使用'会话'

答案 2 :(得分: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的功能以及与您尝试的内容的关系去做。阅读您收到的任何错误消息,并修复他们告诉您错误的内容。

答案 3 :(得分:0)

首先,我认为您可以使用不同模型上的ActiveRecord associations来解决您的问题,以便您提出问题&amp;特定用户的答案:

#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不同的模型,并让它们处理逻辑