Rails Js.Erb File Cannot Find Method "Render"

Rails js.erb file cannot find method render

You're trying to use render from assets. Unfortunately, it isn't possible now, look at this.

How can I render a partial from .js.erb file in rails 4?

Asset Pipeline

Firstly, you need to make sure that you don't use any Rails dynamic path helpers in your javascript directly.

The issue is that if you pre-compile your asset pipeline, you'll typically find these dynamic path helpers won't work correctly. Although there's nothing "wrong" with it - I tend to keep Rails back-end code completely separate from the front-end, as to ensure the versatility of the application:

#app/assets/javascripts/application.js
$(document).on("click", ".element", function(){
$.ajax({
type: "GET",
url: "/cookbooks"
});
});

This will send a "naked" Ajax request (no params / body) to your controller backend:

#app/controllers/cookbooks_controller.rb
Class CookbooksController < ApplicationController
def index
@cookbooks = Cookbook.all
respond_to do |format|
format.html
format.js #-> loads /views/cookbooks/index.js.erb
end
end
end

The trick here is that you can now populate your index.js.erb file with the params necessary to render the partial:

#app/views/cookbooks/index.js.erb
$("#property").html("<%=j render :partial => 'recipes/package_form' %>")

--

This should work for you

Issue when trying to render a js.erb partial template

You seem to have completely misunderstood how JS.erb templates work.

You don't reference .js.erb templates with javascript_pack_tag. Thats for referencing assets in the assets pipeline. And you cannot render partials in the assets pipeline since they are compiled at deploy time.

The reason you are just getting a literal string "<%= j render(partial: 'teacher_form') %>" is that assets are not sent through ERB in webpack. Sprockets would have done it and raised an error.

Instead you would write your js.erb template so that it alters the page:

def show
@teachers = @year.teachers
respond_to do |format|
format.html
format.js # renders show.js.erb
end
end

// You can't use imports since this is not run in the assets pipeline
// you have to make jQuery a global
$("#teacher-form").html("<%= j render(partial: 'teacher_form' %>");

When you click the link the Rails ujs driver actually just loads that script and evaluates it on the page. The premise is that this lets you do ajax requests and SPA like stuff without a SPA framework or actually writing request handlers. If its a good idea or not is debatable.

What I don't understand is why you don't just put the form in the view in the first place and just toggle its visibility instead of making the server complicit it what is just a client side transformation.

Template is missing (application cannot find a .js.erb file)

The reason why it cannot find your template is because, if you notice on the railscast episode, he is doing an AJAX request for a datatype of script

$('#new_painting').fileupload
dataType: "script"

So depending on how you are doing your link to the create action that link probably expects a html format to be rendered.

Template is missing
Missing template users/create, application/create with {:locale=>[:en], **:formats=>[:html]**, :handlers=>[:erb, :builder, :coffee]}. Searched in: * "/home/tim/fairym/app/views"

You can fix this by adding a render js to your action or if you are doing an ajax request make sure your datatype is set to script.

Rails 3.0 - js.erb does not get rendered

If you are trying to access the index method through the browser it won't work because it's not an ajax request .
If you want to try and learn AJAX make a link to another method that has the remote=true attribute , that will send an AJAX request .
Here is how your controller should look like

def index 
end

def show
end

And the views:

index.html.erb

<%= link_to  "Click here" , "/controller/show" , remote: true %>

show.js.erb

alert("Hello ,AJAX!");

Then start playing with more complex stuff .

External link to controller won't render .js.erb file Rails 4

You need to specify format within your url:
http://localhost:3000/posts/11.js

To generate such a route "rails way" pass format option to path:

link_to post.title, post_path(id: post.id, format: :js)


Related Topics



Leave a reply



Submit