Trouble on Rendering a Template Passing a Local Variable

Trouble on rendering a template passing a local variable

:locals => { :user => @current_user }

and in template

Name: <%= user.name %>

Local variables are local, so you don't need @ to refer them.

@user502052
You can render view explicitly from your controller.

render :template => "users/show", :locals => {...}

When you don't execute render in controller, framework does that for you with default parameters. When you do, you can specify different template file, pass local variables, render a partial: anything render function supports.

Missing template when rendering a collection using local variables

If you're trying to render the same partial at app/views/categories/_category.html.erb you need to change your render call for your organization's show template.

render partial: "categories/category", collection: @categories, ...

The app/views/organizations/show.html.erb template will otherwise look for a file at app/views/organizations/_category.html.erb.

Is it possible to render a partial by passing a local variable for the collection?

Please try it

<%= render partial: 'item_kanban', collection: items, as: :item %>

Now you can access

<%= link_to item, class: "list-group-item" do %>
<%= item.number %>
<% end %>

When you did

<%= render partial: 'item_kanban', collection: items %> will give you a item_kanban local variable and not a item variable.

So you need to specify it by as: :item now you can access item

Error when rendering a partial (RoR) passing the form as a local variable

That's weird. It should work as you have it with :form or :f. If you're using Ruby 1.9, maybe there's some difference in how local variables work. I would try :locals => { "f" => f } with a string key instead of symbol and see if that helps. Also, does it work to use a simpler case like :locals => { :x => 2 }?

rails 4.1 - rendering nested partial in JS template - local variable issue

Change your contents partial to this

<div id="contents">
@contents.each do |c|
<%= render partial: "content", locals: {c: c}
end
</div>

Your content partial would be like this:

some html ... (image being rendered by amazon s3/cdn)
<%= render 'action_buttons', :c => c %>

Now in your create.js.erb file you will have this code:

$("#contents").append("<%=j render partial: 'content', locals: {c: @content} %>");

Update:

You can't update just actions as it's inside a loop so you have two options either to render your whole partial again or use js to target your buttons and change there content but since you don't want to render your whole partial so you are left with second option. To update links content your create.js.erb would be like this:

$("#contents").append("<%=j render partial: 'content', locals: {c: @content} %>");
$(".follow").text('following'); // give class to all of your follow buttons and then change there content to following


Related Topics



Leave a reply



Submit