Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rendering a partial from a controller in rails

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.

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

My Controller

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

Thanks!!

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.

like image 607
nathan Avatar asked Nov 13 '12 19:11

nathan


People also ask

What is the partial render in the Rails?

Rails Guides describes partials this way: Partial templates - usually just called "partials" - are another device for breaking the rendering process into more manageable chunks. With a partial, you can move the code for rendering a particular piece of a response to its own file.

What is the difference between render and Redirect_to in Rails?

There is an important difference between render and redirect_to: render will tell Rails what view it should use (with the same parameters you may have already sent) but redirect_to sends a new request to the browser.

How can you tell Rails to render without a layout?

By default, if you use the :plain option, the text is rendered without using the current layout. If you want Rails to put the text into the current layout, you need to add the layout: true option and use the . text. erb extension for the layout file.

What is Local_assigns?

local_assigns is a Rails view helper method that you can check whether this partial has been provided with local variables or not. Here you render a partial with some values, the headline and person will become accessible with predefined value.


1 Answers

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.

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

Then, in 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.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.

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.

like image 176
Tim Sullivan Avatar answered Sep 28 '22 01:09

Tim Sullivan