Rendering a Partial from a Controller in Rails

Rails render of partial and layout in controller

Partial rendering in a controller is most commonly used together with Ajax calls that only update one or a few elements on a page without reloading. Rendering of partials from the controller makes it possible to use the same partial template in both the full-page rendering (by calling it from within the template) and when sub-page updates happen (from the controller action responding to Ajax calls). By default, the current layout is not used.

This may be the reason your code is not working you can use rendering template.

Template rendering works just like action rendering except that it takes a path relative to the template root. The current layout is automatically applied.

if resource.company_form
render :template => "shared/company_signup_form"
else
render :template => "shared/individual_signup_form"
end

** REMOVE UNDERSCORE from your partail name because you are using this as a template.

Hope this works !

Rails: How to render partial view from another controller

As far as syntax goes, what you want is the following:

render partial: "posts/upvote"

Instead of sending html data (the buttons you mentioned) from controller to the partial, you should have a conditional statement in each partial that activates one button or another. From there create event handlers for each respective button. That takes care of the issue of Jquery handling internal state.

Rails render partial from controller

The better way would be to call the partials from index.html.erb

<% unless params[:check].nil? %>
<%= render :partial => '/layout/check' %>
<% else %>
<%= render :partial => '/layout/not_check' %>
<% end %>

so your def index would look like this

def index
respond_to do |format|
format.html
end
end

I did not understand what you are trying to do but partial which are related to controller/actions should not be in layout unless they are serving some layout.

How to render partial view of one controller to another controller in ruby on rails

A couple of changes you'll need to make here. One thing I'll recommend before getting into the details is the Rails Guides for Layouts and Rendering (and the Rails Guides in general. The Layouts and Rendering Guide in particular will help you greatly with the VC part of Rails.

Now, on to the details. First, a partial is not a controller action. There is no controller method that corresponds to it. You can go about what you're trying to do in one of two ways, and both ways involve removing the new method from your subscriptions controller first.

First way: Initialize @subscription in your vendor site controller

The simplest way (in term of changes relative to what you already have) is to initialize @subscription in your vendor site controller's index method. Then, the rendered _new partial will know exactly what subscription is.

class VendorSiteController
def index
@subscription = Subscription.new # add this anywhere in the index method.
end
end

Second way: Pass @subscription as a local to the _new partial

This way will involve changes to your controller, your view, and your partial. In your controller, you'll still need to make the change I gave above.

class VendorSiteController
def index
@subscription = Subscription.new # add this anywhere in the index method.
end
end

Next, you'll need to make @subscription a local variable in your form, by simply removing the @.

<%= form_for(@subscription) do |f| %>
<div class="field">
<%= f.hidden_field @subscription.vendor_site_id, :value=>"1" %>
</div>
<!-- no changes to the button tag -->
<% end %>

Finally, you'll need to pass @subscription as subscription to your partial in your index view.

<%= render :partial => "subscriptions/new", :locals => { :subscription => @subscription } %>

While this way involves a lot more changes, it is much more flexible than the first way. For instance, say you want to render more than one new subscription in a page:

<% Subscription.where(condition: true).find_each do |s| %>
<%= render :partial => "subscriptions/new", :locals => { :subscription => s } %>
<% end %>

As another example, maybe you want to use the @subscription variable name for something other than a new subscription, and you're going to use @new_sub for a new subscription instead.

<%= render :partial => "subscriptions/new", :locals => { :subscription => @new_sub } %>

Hopefully my answer helps you out! Feel free to comment if you need further clarification, and be sure to check out those Rails Guides!

Rendering a partial in rails, in two separate places, from two different controllers

Should be with an equal sign: <%= render ... %>.

<% %> executes Ruby code, but does not render a result. Whereas <%= %> executes and renders.

Rendering a partial from a controller in rails

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.

Rendering partial that belongs to another controller

Partials

You must appreciate that Partials are not controller-dependent (being stored in a controllers' view directory does not tie them for use with that controller)

This means if you have the functionality to support the partial in another controller, you should be able to use it in different parts of your app

--

Error

This leads us to the identification of the problem you're receiving.

It's not the calling of the partial which causes an issue - it's how you're referring to the code inside it:

undefined method `lights' for nil:NilClass

The error is clearly that you're trying to call the lights method on an object / variable which doesn't exist. This is defined inside the partial itself here:

@client.lights.map do |c|

Therefore, you need to be able to pass the correct data to the partial, enabling it to load the @client object without being dependent on the controller

--

Fix

To do this, you may wish to consider using partial locals -

<%= render partial: "lights/lights", locals: {client: @client} %>

This means that every time you call the partial, you'll have to pass the @client object into the client local var, thus allowing the partial to run controller-independently.

Here's how you'd handle it in the partial itself:

#app/views/lights/_lights.slim
- client.lights.map do |c|


Related Topics



Leave a reply



Submit