undefined method `formats' when rendering html.erb file - javascript

Im using Rails 3.0.3 and ruby 1.8.7
I want to render /materials/change_prize.html.erb in materials/change_prize.js.erb file.
change_prize.js.erb:
$("#content").html("<%= escape_javascript(render(:action => 'materials/change_prize.html.erb')) %>");
Materials Controller:
def change_prize
#material = Material.find(params[:id])
respond_to do |format|
format.js
format.html
end
end
routes.rb:
resources :materials do
member do
get 'change_prize'
post 'change_prize'
end
end
but when i load the js file i get this error:
undefined method `formats' for nil:NilClass
what am i doing wrong?

You need to use :partial not :action
<%= escape_javascript(render :partial => "materials/change_prize", :locals => { :xxxx => #xxxx }) %>
n.b. xxxx is if you need to pass your object to the partial

Partials have to begin with underscore. So rename your filename to "materials/change_prize.html.erb" to "materials/_change_prize.html.erb". Do note however that when calling it thru the render call you refer to the file WITHOUT the underscore.
$("#content").html("<%= escape_javascript(render(:action => 'materials/change_prize.html.erb')) %>");
Filename : app/views/materials/_change_prize.html.erb

Related

Rails remote javascript

In my views I have recently started implementing remote javascript updates of only parts of my pages using commands such as:
$("#flash_messages").html("<%= escape_javascript(render 'shared/flash_messages') %>");
I now have a scenario where I have a list of items, each one within tags with a unique id and when the user clicks on a link within an item I want to only replace the contents of the corresponding
i.e.
<span id="article-123">
html here
</span>
create.js.erb
$("#???????").html("<%= escape_javascript(render 'shared/article') %>");
My question is how to I pass the span id "article-123" through to my create.js.erb file and include it as a variable in the above command.
Try with locals variable in render response, in YourController#method
respond_to do |format|
format.js { render "create", :locals => {:article_id => article_id} }
end
now you can use article_id in create.js.erb file.

Rails / Javascript - render partial is breaking params check

I have a partial that I'm rendering via javascript:
$('#calendar').html('<%= escape_javascript(render :partial => "schedules/calendar", :locals => {:schedule_tasks => #schedule_tasks}) %>');
and everything works but this one line doesn't work, the params[:day] is in the original URL string:
<%= params[:day] == day.to_s ? 'green':'' %>
Is there something special I need to do to pass in the params[:day]? It's still in the URL.

How to Load a different js file if controller action result is nil

Here is my controller action:
*controller/books_controller.rb*
def search_book
#found_book = Book.find_by_token(params[:token_no])
#once found I am rendering my search_book.js.erb file (with UJS)
respond_to do |format|
format.js
end
end
Here is my js file to alter my html contents : views/books/search_book.js.erb
$("#found_book").html("<%= escape_javascript( render(:partial => "book_view") ) %>");
If my book is successfully found then rendering a partial via UJS.(Done)
Let's say if my book is not found i.e. if #found_book.nil? is true.
In that case I want to render another partial for my div "found_book". How to do that ?
Remember that search_book.js.erb is still erb and thus you can evaluate your instance variable and use a conditional to render the partial you choose.
<%- if #found_book.nil? %>
$("#found_book").html("<%= escape_javascript( render(:partial => "no_book_view") ) %>");
<%- else %>
$("#found_book").html("<%= escape_javascript( render(:partial => "book_view") ) %>");
<% end %>

Rails double render

I know this isn't valid code, but is there a way to do something like this in Rails?:
render "$('#dialog').replaceWith(#{render :action => 'new.html.erb'});"
What I'm trying to do basically is replace the contents of a JS dialog with what is/would be returned from calling render 'new.html.erb'.
Edit for #Devin M:
controller action:
def new
#act = Act.new(:user_id => current_user.id)
end
def create
#act = Act.new(params[:act])
if #act.valid?
#act.save
else
render :action => :new
end
end
new.js.erb:
$('#dialog').replaceWith("<%= escape_javascript(render(:action => 'new.html.erb')) %>");
Full error:
Showing app/views/acts/new.js.erb where line #1 raised: undefined method `formats' for nil:NilClass
You should split this code out into a seperate view since including it in the controller would go against the ideas of MVC. I would update the controller to respond to JS requests using some code like this in the action I wanted to modify:
respond_to do |format|
format.html { redirect_to #item }
format.js
end
And create a view like this with the extenstion .js.erb:
$('#dialog').replaceWith("<%= escape_javascript(render :partial => "new.html.erb", :locals => { :act => #act }) %>");
You can then trigger this JS with a remote link to the action or by adding your own UJS.

How do I include HTML in a JS Rails response?

I have a FooController that responds to HTML and JS (AJAX) queries:
# app/controllers/foo_controller.rb:
class FooController < ApplicationController
layout 'foo'
def bar
respond_to do |format|
format.html # foo/bar.html.erb
format.js # foo/bar.js.erb
end
end
end
The templates to support it:
# app/views/layouts/foo.html.erb:
<html>...<%= yield %>...</html>
# app/views/layouts/foo.json.erb:
<%= yield %>
And an AJAX template in which I want to render a partial:
# app/views/foo/bar.js.erb:
dojo.byID('some_div').innerHTML = "<%= escape_javascript(render(:partial => 'some/partial')) %>";
If the JS template just has plain old JS in it (like alert('hi');), it uses my JS template. When I put in the render(:partial), though, it makes the whole response use the HTML template, which means it's no longer valid JS.
A possible solution is to use a function for the layout:
class FooController < ApplicationController
layout :choose_layout
...
private
def choose_layout
return nil if request.xhr?
'foo'
end
end
But my version should work! Why doesn't it?
The most recent Railscast covers this topic (using jQuery).
I'm not quite seeing where you might be going wrong, but here's a snippit from the Railscast that works just fine to render a partial:
// views/reviews/create.js.erb
$("#new_review").before('<div id="flash_notice"><%= escape_javascript(flash.delete(:notice)) %></div>');
$("#reviews_count").html("<%= pluralize(#review.product.reviews.count, 'Review') %>");
$("#reviews").append("<%= escape_javascript(render(:partial => #review)) %>");
$("#new_review")[0].reset();
Where are you storing your Javascript? Do you have an Application.js that you're keeping things in? If so, are you including "dojo" before "application" in your javascript_include_tag?
Try the following;
class FooController < ApplicationController
layout 'foo'
def bar
respond_to do |format|
format.html
format.js { render :layout => false }
end
end
end
Hope that helps.
J.K.

Categories