I’m trying to simply render a view using UJS and Rails 3, but it’s not actually working. I’ve read many tutorials but can’t seem to get this. Here are some code snippets:
index.js.erb
"<%= render user_path(@this_user) %>"
index.html
<%= form_tag users_path, :method => :get, :remote => true do %>
<%= submit_tag "Test", :name => nil, :class => "btn" %>
<% end %>
users_controller.rb
def index
@users = User.all
@this_user = User.find(1)
respond_to do |format|
format.html # index.html.erb
format.js
format.json { render json: @users }
end
end
Every time I click on “Test”, I get the following output:
Started GET “/users?utf8=%E2%9C%93” for 127.0.0.1 at 2012-10-19
11:31:49 -0700 Processing by UsersController#index as JS Parameters:
{“utf8″=>”✓”} User Load (0.4ms) SELECT “users”.* FROM “users”
User Load (0.2ms) SELECT “users”.* FROM “users” WHERE “users”.”id” =
$1 LIMIT 1 [[“id”, 1]] Rendered users/index.js.erb (1.8ms)
Completed 500 Internal Server Error in 6msActionView::Template::Error (The partial name (/users/1) is not a
valid Ruby identifier; make sure your partial name starts with a
letter or underscore, and is followed by any combinations of letters,
numbers, or underscores.):
1: “<%= render user_path(@this_user) %>” app/views/users/index.js.erb:1:in
_app_views_users_index_js_erb__2145063269526192522_70203075823780'index’
app/controllers/users_controller.rb:9:in
It appears it’s looking for a partial; however I just want it to execute the view associated with the “show” action on the User controller. So a completely new page.
What am I doing wrong? First step is to render a completely new view, but I plan to render a partial view within the “show” page after I figure this out.
Thanks in advance for any help!
Look at index.js.erb:
That is telling Rails to render a partial with the name of the value of
user_path(@this_user).You view file
index.js.erbneeds to return JavaScript. So, the following would work:In the future, you can render a partial (that contains JavaScript code) by doing the following:
$("#popup").html("<%= escape_javascript(render(:partial => 'show_sold_out_popup') %>");