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

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

问题描述

我有一个是通过远程=>真正添加行到数据库的形式。然后,我要追加新数据表,但不能得到正确的观点来呈现。

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

我只需要做出一个替代放映视图,然后告诉这个在redirect_to这一观点。

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

然后,在意见/任务/ create.js.coffee

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

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

什么是怎么回事的是,浏览器会调用 create.js 。控制器与 create.js 模板响应,因为的respond_to 块的 format.js的, 。该Ĵ _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.

这一切有点如果您使用的是客户端的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.

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

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