Rails: Pass a parameter to a render js function - ruby-on-rails-3

I have this in my controller
...
..
render :js => "window.location = '/bar/ready'", :locals => { :foo => "foo" }
..
...
I want the <% foo %> variable and I want to access it in my HTML page.
but I cant seem to access it, as <%= params.inspect %> returns
{"controller"=>"bar", "action"=>"ready"}
So how can I pass the variable in my HTML page?

I needed something similar. Late, but I finally manage to do it doing:
render :js => "window.location = #{your_path(:foo => 'foo').to_json}"
And then parse json data in your view

Related

Partial not accessing local variable

I am rendering a partial like so:
<% #pages.each do |page| %>
<%= render 'layouts/pagewithchildren', :locals => { :page => page } %>
<% end %>
But when i try to access a variable in page i am getting the error:
undefined local variable or method `page'
I am accessing the variable like:
<%= page.title %>
So what else do I need to do?
i'm not 100% sure but isn't it either
<%= render 'layouts/pagewithchildren', :page => page %>
or
<%= render :partial => 'layouts/pagewithchildren', :locals => { :page => page } %>
?
You have to explicitly specify partial, otherwise, Rails will treat locals as a params hash, you can access locals[:page] but not page variable directly in your partial.
Change your code to:
<%= render partial:'layouts/pagewithchildren', locals: {page: page} %>

Render #object and locals vs render :partial

I want to pass a local variable that contains the origin to come on a specific page, this variable contains just a symbol with the value.
When I use this code it works perfect, the origin variable is accessible in the partial :
render :partial => "products", :collection => #products, :locals => {:origin => :gallery}
But when I use this code, the origin is not set and not accessible in the partial :
render #products, :locals => {:origin => :gallery}
What is the difference here? Is the second line of code not render the partial like the first line?
<%= render #products %>
Is indeed the shorthand syntax for rendering a partial. But with the shorthand syntax, Rails will ignore the ":locals" variable. There's more on this in the Rails Guides.
So if you want to pass extra options to the render, you have to specify ":partial => ...". If you want to know why this happens, you can take a look at the Rails source.
There's a good explanation here: Rails: confused about syntax for passing locals to partials
The short version is that you can just omit :locals in the second example:
render #products, :origin => :gallery

Rails3: How to pass param into custom will_paginate renderer?

I've got a custom will_paginate renderer that overrides WillPaginate::ViewHelpers::LinkRenderer's link method like so:
def link(text, target, attributes = {})
"<a href='/users/95/friends_widget?page=#{target}' rel='next' data-remote='true'>#{text}</a>"
end
...and that works great, except you can see the hard-coded 95 in that link. How would I pass a parameter (e.g. user or user's ID) into the custom renderer via the Rails view?
<%= will_paginate(#user_friends, :remote => true, :renderer => FriendsRenderer) %>
Or is there something I'm missing, some easier way to do it?
BTW: #user_friends isn't available in the custom renderer, and I've already tried just adding params onto the end of that will_paginate call, e.g. :user => user)
will_paginate lets you pass in :params for the links:
will_paginate(#user_friends, :params => { :user_id => 95 })
View:
<%= will_paginate #user_friends, :renderer => 'FriendsRenderer',
:remote => true,
:link_path => friends_widget_user_path(#user) %>
class FriendsRenderer < WillPaginate::LinkRenderer
def prepare(collection, options, template)
#link_path = options.delete(:link_path)
super
end
protected
def link(page, text, attributes = {})
# Here you can use #link_path
end
end
Note that this works for the will-paginate version: 2.3.6

Rendering the Devise edit Password Form

I'm trying to render the Devise edit password form within another view because I don't want to duplicate the edit pw logic.
I've tried the following (after generating the Devise views):
<%= render 'devise/passwords/edit' %>
<%= render 'devise/passwords/form' %>
And a number of other variations on render that all seem to give me the same error:
"ActionView::MissingTemplate in foo#foo
Missing partial devise/passwords/edit..."
This variation:
<%= render :file => 'devise/passwords/edit.html.erb' %>
Gave me some hope but the following error:
"undefined local variable or method `resource' for #<#:0x47ef0e0>"
around this line:
<%= form_for(resource, :as => resource_name, :url => password_path(resource_name), :html => { :method => :put }) do |f| %>
That makes me think I'm close (as that is code from the form that I want) but shouldn't that template be using the correct logic from the hidden Devise controller? Or do I need to do something in the routes file to get this to work?
Am I way off?
Try this:
<%= render :template => 'devise/passwords/edit',
:locals => {
:resource => my_user_model_variable,
:resource_name => my_user_model_name } %>
Where:
my_user_model_variable could be current_user
my_user_model_name could be "User"

Render partial in layout file with data

The navigation for my site is obviously stored in the application layout file. Part of that navigation is driven by the database. How to I render a partial in the layout and pass in the collection of objects for it to render?
EDIT:
I think my question revolves more around how to get data into the partial, is that done form the application controller or do I have to add the data in each action on each controller?
If you just wish to pass one object in then you can use the object key on render. The passed object will be accessible as an local variable of the same name as the partial. SO if the partial is called navigation the local variable will be navigation.
<%= render :partial => 'foo/navigation', :object => #my_collection
In the partial:
<% for obj in navigation %>
...
<% end %>
If you wish to pass multiple objects then you can use the locals key. The names of the local variables in your layout are then the keys of the passed hash.
<%= render :partial => 'foo/navigation', :locals => { :foo => 'Hello', :bar => 'World' }
In the partial:
<%= foo %>
<%= bar %>