简体   繁体   English

rails respond_with相关记录

[英]rails respond_with associated record

for example, there's a Question model that has_many :answers 例如,有一个Question模型has_many :answers

I'd like to show a form for new answer on the questions#show ( just like stackoverflow does ) 我想在questions#show #show上显示一个新答案的表格(就像stackoverflow一样)

routes would be something like: 路线将是这样的:

resources :questions do
  resources :answers
end

What is the best way to show the errors for an invalid record under these circumstances? 在这些情况下,显示无效记录的错误的最佳方法是什么?

The problem is that I can't render an action I need from within AnswersController ( since it would be questions#show ), the only way seem to be redirect_to and store errors in flash hash. 问题是我无法在AnswersController render我需要的动作(因为它会是questions#show AnswersController ),唯一的方法似乎是redirect_to并在flash hash中存储错误。

It just looks like a pretty much common scenario, I believe there should be some better way 它看起来像一个非常常见的场景,我相信应该有更好的方法

This may be one of a few cases where it's actually justified to add a new resourceful member route to your QuestionsController : 这可能是少数情况之一,实际上有理由将新的资源丰富的成员路由添加到QuestionsController

resources :questions do
  post 'answer', :on => :member
end

which would recognize question/:id/answer with a POST request routed to questions#answer , allowing you keep all the logic in one controller: 这将识别question/:id/answer ,POST请求路由到questions#answer ,允许你将所有逻辑保存在一个控制器中:

class QuestionsController < ApplicationController
  ...
  def show
    @question = Question.find(params[:id])
  end

  def answer
    @question = Question.find(params[:id])

    @answer = @question.answers.build(params[:question][:answer])

    if @answer.save
      # show question with newly posted answer at url /question/:id
      redirect_to @question
    else
      # show question with invalid editable answer at url /question/:id/answer
      render 'show'
    end
  end
  ...
end

Explanation: In my opinion, the decision to handle the logic in one controller as opposed to two comes down to what you consider to be the resource of interest. 说明:在我看来,在一个控制器中处理逻辑而不是两个控制器的决定归结为您认为是感兴趣的资源。 Normally, you would consider each model to represent a distinct resource and thus create a separate controller to handle actions related to each resource. 通常,您会考虑每个模型来表示不同的资源,从而创建一个单独的控制器来处理与每个资源相关的操作。 However, when there are multiple deeply coupled models for which multiple actions (eg show , new , create ) are handled in a single view, it might be cleaner to think of the models as forming a single resource. 但是,当存在多个深度耦合的模型时,在单个视图中处理多个动作(例如shownewcreate ),将模型视为形成单个资源可能更清晰。

In this example, I think of the resource as a collective one consisting of both the question and its answers. 在这个例子中,我觉得资源作为一项集体同时包含问题和答案的。 Since this collective resource is uniquely identified by the question itself, I would let the question controller handle it. 由于这个集体资源是由问题本身唯一标识的,我会让问题控制器处理它。 The show action in the questions controller already involves retrieving the collective question-answers resource, so you might think of the answer action (and potentially unanswer and reanswer actions) as the analogue of update to that collective resource. 问题控制器中的show动作已经涉及检索集体问答资源,因此您可能会将answer操作(以及可能的unanswerreanswer执行操作)视为对该集合资源的update的类比。

Thinking of the resources this way is largely a matter of design preference, and there will be trade-offs depending on the requirements. 以这种方式考虑资源主要是设计偏好的问题,并且将根据要求进行权衡。

You can render questions#show from the AnswersController like this: 您可以像这样从AnswersController渲染questions#show AnswersController

render :template => 'questions/show'

If you want to jump to a specific anchor on the page, you have to define that in your answer form: 如果要跳转到页面上的特定锚点,则必须在答案表单中定义:

<%= form_for(@answer, :url => question_answers_url(@question, :anchor => 'answer_form')) do |f| -%>

Have a partial which has the form which posts data to the answers_controller#create action. 有一个部分具有将数据发布到answers_controller #create动作的形式。

So, in your questions#show page - show.html.erb, render a partial 所以,在你的问题#show page - show.html.erb中,渲染一个部分

<%= render :partial => "answers/answer_form" %>

In the _answer_form.html.erb, have a form which will post data to answers#create 在_answer_form.html.erb中,有一个表单,可以将数据发布到答案#create

<% form_for @answer do |f| %>
  # have a text area
<% end %>

As far as the flash is concerned 就闪光而言

In the AsnwersController 在AsnwersController中

def create
  @question = Question.find(params[:question_id])
  @answer = Answer.build(params)
  if @answer.save
    flash[:notice] = "Answer was posted successfully"  
  else
    flash[:error] = "There were a few errors please try again"  
  end
  redirect_to question_path(@question) 
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM