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

问题描述:

我有一个“问题和答案”部分,如果答案不是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'实例变量在视图中有什么,你可以使用''输出它的内容yaml格式。 2)你的'AnswersController'可以用'before_action:load_question'来清理,它会为'AnswersController'上的每个动作(或者只有你想要的)找到并设置'@ question'。 –

+0

我们可以看到“问题”和“答案”表的模式吗? – kristinalim

+0

@ kristinalim架构已添加。只有一个问题表,没有答案。 – xps15z

好像要表明,用户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 %> 

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

首先,我想你可以用在不同型号的一些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

它只是可以使用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

有很多的事情,我不明白为什么你在你的代码做的,所以你可能需要修改,使之适合你的目的,但这是我认为你想要的。

我假设您的“用户个人资料页面”是您通过转到如/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