2

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

4 回答 4

2

您似乎想根据提供的接收者 ID 向用户展示问答。如果是这种情况,试试这个......

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

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

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

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

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

于 2013-10-31T16:56:13.400 回答
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 的作用以及它与您尝试做的事情之间的关系。阅读您收到的任何错误消息并修复他们告诉您的错误。

于 2013-10-30T17:25:40.760 回答
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 ' 是使用 ' session '的辅助方法

于 2013-10-30T11:52:08.053 回答
0

首先,我认为您可以通过不同模型上的一些ActiveRecord 关联来解决您的问题,以便您为特定用户提取问题和答案:

#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 约定鼓励“胖模型,瘦控制器”,这意味着你真的应该只使用控制器来 ping 不同的模型,让它们处理逻辑

于 2013-10-28T09:33:58.183 回答