从 rails 中的控制器渲染部分 [英] Rendering a partial from a controller in rails

查看:26
本文介绍了从 rails 中的控制器渲染部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

我的控制器

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

谢谢!!

编辑我认为我需要的只是另一种展示"视图.

EDIT I 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.

推荐答案

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

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.

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

The partial should be rendered within a JavaScript response, not the controller. The controller looks more like 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 }
        format.js
      else
        format.html { render action: "new" }
        format.json { render json: @task.errors, status: :unprocessable_entity }
        format.js
      end
    end
  end

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

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

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

这里发生的事情是浏览器调用了create.js.由于 respond_to 块的 format.js,控制器以 create.js 模板响应.j 转义 _newly_ added.html.erb 文件的内容,并将其内容附加到表中.控制器不与现有视图交互,而是将 JavaScript 发送到浏览器,并与视图交互.

What's going on here is that the browser makes a call to create.js. The controller responds with the create.js template, because of the respond_to block's format.js. The j escapes the contents of the _newly_added.html.erb file, 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.

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

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.

这篇关于从 rails 中的控制器渲染部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆