Rails: Render in *.js.erb doesn't execute - ruby-on-rails-3

I have this code in my js.erb file
$("<%=j render(:partial => "phone/phone_fields" , :phone => #phone ,:token => tokenizer, :exists => false) %>").hide().appendTo('.contacts').slideDown();
The controller that calls it passes the #phone variable while tokenizer is a helper. If I make tokenizer and #phone as an alert message it works but when i use it inside a render statement, it does nothing. It doesn't execute at all. It doesn't even give an error or something..
I have tried many variations such as calling the js render statement inside the controller and the result is passed down to the js file and still doesn't work. But i was wondering why it works in one of my helpers.

Try this while rendering partial from "js.erb" files :
<%= escape_javascript(render(:partial => "phone/phone_fields",
:phone => #phone ,:token => tokenizer, :exists => false)) %>

Related

Rails: Pass a parameter to a render js function

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

Accesing to my carrierwave model object with an attribute name

I'm trying to put a file link with a partial render, something like this:
#Main haml file
= render :partial => 'file_upload', :locals => {:f => f, :file_download => 'residence_cert'}
#Inside the partial
%a{:href => #postulant_info[file_download]}
But this is making a link just with the file name, not the full path to the file. Then realized that #postulant_info[file_download] is just giving me a String with the file name, not the carrierwave object
-logger.debug #postulant_info.residence_cert #this is returning my carrierwave object -> 'DocumentUploader'
-logger.debug #postulant_info['residence_cert'] #but this one is just returning a String, the DB record with the file name
One solution could be put the url in the render as a local like = render :partial => 'file_upload', :locals => {:f => f, :file_download => 'residence_cert', :url => #postulant_info.residence_cert.url} but I think "should" be unnecessary when you have the attribute name inside the partial template.
Any thought would be appreciated. Thanks in advance
I'm extremely uncertain about what your problem exactly is, and what you're trying to achieve, but if you want to output a link to a file, in your partial then
%a{:href => #postulant_info.residence_cert.url}
is enough. #postulant_info.residence_cert will give you an instance of the uploader (DocumentUploader), and the #url method returns the complete url of the uploaded file
By the way, you should avoid using instance var in your partials, the only variables a partial should use are the it's been given.
At last I could achieve it with:
%a{:href => #postulant_info.send(file_download)}

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

Share resources between layouts in Rails 3

OK, so Ive set up my mailer in Rails which works fine, but I wanted to make a new action (or maybe just a view?) to have a slimmed down contact form in a lightbox. I can do that all fine and dandy but it would use the default layout which I dont want. So I added:
render :layout => 'lightbox'
to the action so that I could use a new layout. Unfortunately that seems to block off my access to the model as I get this error when the lightbox pops up
undefined method `model_name' for NilClass:Class
#on this line
<% form_for #contact, :url => {:action => "create"}, :html => {:method => :post} do |f| %>
So by using a different layout I cant use the resources I set up in my routes which is here:
resources :contacts, :only => [:new, :create], :as => :contacts
#Im passing in a name to the email form
match "contacts/direct/:name" => "contacts#direct", :as => :direct_email
I hope that made sense. But what do I do?

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