Sunday, December 5, 2010

[Rails] rails3 render partial with unobtrusive js

Some things seem like they will never be clear with Rails. It took me
half a day to figure out how to render a partial using unobtrusive js,
but it is not clear why it works and why it went through all the
singular plural stuff.

In the show view of a model, I wanted to display two related lists/
indexes (with a :remote => true link to load them). One of the items
has a controller, but I normally just create a partial to display the
list because all the features in the index view may not apply. The
other item does not have a controller and lives within its parent
model.

the routes:
resources :stages do
resources :assessors, :only => [:index, :new, :create]
member do
get "import"
get "applicants"
end
end

Applicants live only with stages and are dynamically created/updated.

Following the Railscast on unobtrusive JS and trying to turn it into
something that would load a partial, nothing seemed to work. I could
not access my _applicants.html.erb with js, it wanted it in the
assessors view. I finally got an error on the log that said, not only
does it want it in the assessors view directory, it wants it named
_assessor - even thought it is a list.

Same thing with the applicants. It would take the applicants.js.erb
file in the stages directory, but wanted a singular partial in the
applicants view - which I did not have.

The JS line is:

$("related").update("<%= escape_javascript(render(@applicants))%>");

The render call won't accept partial, file name or about anything else
I tried.

I know there is not a lot out there yet on rails3 and unobtrusive JS,
but if someone could enlighten me, I'd appreciate it.

Steve


--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate