I'm working with rails 3.1.0 and this is my first application on 3.1.0
I have a remote link:
link_to "my link",{:controller=>"my_controller",:action=>"my_action"},:remote=>true
and in my_controller I have
def my_action
#data = Data.all
render :update do |page|
page.replace_html "show_data",:partial=>"data_partial"
end
end
but then in the log I get an error
ActionView::MissingTemplate (Missing template my_controller/update...
and I was checking at this post
http://wowkhmer.com/2011/09/19/unobtrusive-ajax-with-rails-31/
do I really need to use a coffee script or a js.jrs to do this thing ??
Javascript integration doesn't work this way anymore. render :update ... tries to render the update action, which doesn't have an associated template. You need to move that out of the controller and into the view code, in app/views/my_controller/my_action.js.erb:
$("show_data").update("<%= escape_javascript(render :data_partial) %>");
Related
I have an interesting situation. I am testing the following simple create action:
# will only be accessed via Ajax
def create
click = Click.new(params[:click])
click.save # don't really care whether its success or failure
end
Then I have the following very simple controller spec:
require 'spec_helper'
describe ClicksController, "creating a click" do
it "should create a click for event" do
xhr :post, :create, :click => {:event_id => 1}
# more test to come...
end
end
Seems trivial, yet I get the following:
Missing template clicks/create
Any tips would be appreciated.
Add to the controller action:
render :nothing => true
This one will automatically create the appropriate server's respone. More here
You will get this error if your controller renders only JSON or XML, yet you don't specify a format in the spec; your request then defaults to unsupported HTML. In that case, simply specify the supported format when you invoke the controller method from your spec. For example, change this:
post :create, registration: #user_hash
to this:
post :create, registration: #user_hash, format: :json
If you do not render anything in a controller action, rails will attempt to default to rendering a template (in this case clicks/create). I'd suggest rendering back at least a success message like so:
render :json => {:success => true}
Building on megas's answer, if you're looking to test a controller action that's only accessed via a UJS link and only has a .js.erb template, I'd put this in the controller to avoid breaking your UJS functionality:
respond_to do |f|
f.html { render nothing: true } # prevents rendering a nonexistent template file
f.js # still renders the JavaScript template
end
This will enable you to call the controller action by simply calling ActionController::TestCase::Behavior's get/post/put/delete methods instead of needing to call xhr, because it will successfully call the method, render nothing, and continue on, while leaving your UJS behavior intact.
I have the following code as a controller action:
def create
render :js => "alert('hello')"
end
But when I go to this view, I get a missing template error – 'create.rhtml'. What is the correct way to render a javascript response in Rails 1.2.6?
render :file is probably your best bet with Rails that old. You can also use the old RJS style.
I'm trying to learn how to use AJAX in my rails apps so i've decided to start with something simple. I have a blog app on which user can vote on any blog post. Here is my code for posts#vote:
posts_controller.rb
class PostsController < ApplicationController
(...)
def vote
post = Post.find(params[:id])
if current_user.voted_on?(post)
current_user.unvote_for(post)
else
current_user.vote_for(post)
end
respond_to do |format|
format.html { redirect_to post_path(post) }
format.js
end
end
end
and here is a link code for my posts#view:
view.html.erb
<%= link_to "Vote", vote_post_path(post.id), :remote => true %>
And now, if i'll click my Vote link, posts#vote action works and vote is casted, however i'm getting an error:
ActionView::MissingTemplate (Missing template posts/vote,
application/vote with {:handlers=>[:haml, :coffee, :erb, :builder],
:locale=>[:en], :formats=>[:js, :html]}.
I have (empty) vote.rjs file in my views/posts folder but for some reason rails can't see it. According to error, the only file extensions that rails is searchng for are .haml, .coffee, .erb and .builder. Shouldn't there be also a .rjs extension on that list? Thanks in advance.
Your file should be called vote.js.erb. Rails doesn't use a .rjs extension.
The .rjs extension was originally used for Rails and Prototype JS library.
With Rails 3.1 the default library was switched to JQuery.
If you want to use .rjs in your Rails project it would mean using Prototype instead of JQuery. The gem for that is prototype-rails.
I'm using form_remote_tag(:url => {:controller => "home", :action => "search"}, :update => "mydiv"). When I click submit on the form "mydiv" is populated with the error "Template is missing. Missing template home/search.erb in view path app/views". I've tried multiple render options in def search, but they all result in the same error.
It looks like the search method is trying to use it's default render even though I'm specifying what I want.
I've tried:
render 'index'
render :text => 'Return this from my method!'
Is my url incorrect? Is it not submitting back to my home controller's search method?
Try
render :action => 'index'
this will use "index.rhtml" or "index.html.erb".
I will try to explain why it said search.erb is not found, lets take create action for a some model, if there is some error in my create action they it will throw missing template create.html.erb file, since you have some error in your create action rails will try to render the create.html.erb in the page. Hope I explained it clearly.
In an ajax action you can't use redirect_to or render options directly.
try using this in your search action
render :update do |page|
page.replace_html "ur_div_id","partial"
end
The form_remote_tag needs prototype to function. Make sure you are including the :defaults for your javascript libraries namely prototype.
<%= javascript_include_tag :defaults %>
In my RSpec tests, I need to simulate an AJAX GET request to the index action, and have been using the code as described in both the Rails docs and the RSpec book:
xhr :get, :index
This always fails though, as the test tries to load the show action (without any parameters) rather than the specified index action.
The controller action is:
def index
#contacts = Contact.all
respond_to do |format|
format.html
format.js {
render :update do |page|
page.replace_html :contact_search_results, :partial => 'contacts'
end
}
end
end
The error thrown by running the spec is (showing the :show action being used):
ActionView::TemplateError in 'ContactsController as an administrator user when
showing the index of contacts' as an AJAX request should render results into the
contact_search_results element'
contact_url failed to generate from {:action=>"show", :controller=>"contacts",
:id=>#<Contact id: nil, first_name: nil, ....>}
Does anyone know how I can simulate an AJAX call the index action in tests?
Thanks!
Actually I think you're misunderstanding the error. Somewhere along the way Rails is trying to call contact_url and the parameters are wrong. My suspicion is that it is indeed calling the index action which then renders the contact partial. If I'm right, the contacts partial is the location of the issue. I would recommend reviewing the contacts partial for any possible errors. If you're still having trouble, please post the body of your contacts partial.
You're trying to make a URL to a non-persisted Contact object. You can see that in the message: :id=>#<Contact id: nil