Ruby-on-rails 从 rails 中的控制器渲染部分

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13367426/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 21:01:07  来源:igfitidea点击:

Rendering a partial from a controller in rails

ruby-on-railsrubyajaxcontroller

提问by nathan

I have a form that is adding rows to the DB via remote => true. I then want to append the new data to a table, but cannot get the correct view to render.

我有一个通过远程 => true 向数据库添加行的表单。然后我想将新数据附加到表中,但无法获得正确的视图来呈现。

As of now, it is rendering the entire show.html.erb page for the new entry, but I want to layout a minimal version to be added as a . Is there a quick way to tell my controller what view to render after inserting into the db? I want to render my partial named _newly_added.html.erb

截至目前,它正在为新条目呈现整个 show.html.erb 页面,但我想布局一个最小版本以添加为 . 有没有一种快速的方法可以告诉我的控制器在插入数据库后要呈现什么视图?我想渲染名为 _newly_ added.html.erb 的部分

My Controller

我的控制器

  def new
    @task = Task.new
    render :partial => "/tasks/newly_added", :locals => { :t => @task }
  end

Thanks!!

谢谢!!

EDITI think what I need is just an alternative "show" view.

编辑我认为我需要的只是一个替代的“显示”视图。

I found that the method I needed to change was actually this:

我发现我需要改变的方法实际上是这样的:

  def create
    @task = Task.new(params[:task])

    respond_to do |format|
      if @task.save
        format.html { redirect_to @task, notice: 'Task was successfully created.' }
        format.json { render json: @task, status: :created, location: @task }
      else
        format.html { render action: "new" }
        format.json { render json: @task.errors, status: :unprocessable_entity }
      end
    end
  end

I just need to make an alternative show view, and then tell this to redirect_to that view.

我只需要创建一个替代的显示视图,然后告诉它重定向到该视图。

采纳答案by Tim Sullivan

Edited per the changes in your question. However, nothing really changes. You're thinking about things wrong, and need to adjust how you're thinking. You don't need an alternative show, you need to handle the format.js request.

根据您问题中的更改进行编辑。然而,没有什么真正改变。你在思考错误的事情,需要调整你的思考方式。您不需要替代节目,您需要处理 format.js 请求。

The partial should be rendered within a JavaScript response, not the controller. The controller looks more like this:

部分应该在 JavaScript 响应中呈现,而不是在控制器中呈现。控制器看起来更像这样:

  def create
    @task = Task.new(params[:task])

    respond_to do |format|
      if @task.save
        format.html { redirect_to @task, notice: 'Task was successfully created.' }
        format.json { render json: @task, status: :created, location: @task }
        format.js
      else
        format.html { render action: "new" }
        format.json { render json: @task.errors, status: :unprocessable_entity }
        format.js
      end
    end
  end

Then, in views/tasks/create.js.coffee

然后,在 views/tasks/create.js.coffee

($ '#mytable').append("<%= j render(partial: 'tasks/newly_added', locals: { t: @task }) %>")

What's going on here is that the browser makes a call to create.js. The controller responds with the create.jstemplate, because of the respond_toblock's format.js. The jescapes the contents of the _newly_added.html.erbfile, and the contents of it are appended to the table. The controller doesn't interact with the existing view, instead, JavaScript is sent to the browser, and it interacts with the view.

这里发生的事情是浏览器调用create.js. 该控制器响应与create.js模板,因为respond_to块的format.js。该j脱的内容_newly_added.html.erb文件,并且它的内容附加到表。控制器不与现有视图交互,而是将 JavaScript 发送到浏览器,并与视图交互。

This all changes somewhat if you're using a client-side MVC framework like Backbone or Ember, but you didn't specify that so I'm assuming you're going with stock Rails.

如果您使用像 Backbone 或 Ember 这样的客户端 MVC 框架,这一切都会有所改变,但您没有指定,所以我假设您使用的是 Rails。