I'm having some problems to update my div after submitting a form with Rails 4. Here is the relevant part of my code:
View:
_details.html.haml
= form_tag(url_for(save_answer_path(format: :js)), remote: true) do
(some html code ....)
= submit_tag t('app.bouton.send'), id: 'sendButton'
%div#divlogs
= render partial: 'logs'
Controller:
def save_answer
some code ...
respond_to do |format|
format.js
end
end
JS:
save_answer.js.erb
$('#divlogs').html("<%= escape_javascript(render partial: 'logs') %>");
When I submit, everything is called correctly but I'am getting an incorrect output. What I want is to update my div, but instead I get a page with what I have on my JS file, but with the content of my partial.
Example:
My URL after submitting:
http://domaine/controller/save_answer.js
What I get on the screen:
$('#divlogs').html("<p>This should appear on the div </p>");
Does anyone know what is going on?
Solution:
As #VladKhomich said in the comments above, I was missing the JS file rails.js.
You can download it from here: https://github.com/rails/jquery-ujs/blob/master/src/rails.js
Related
I'm making AJAX based application with Devise as authentication system. The problem I encountered is that when I sign in with Devise with AJAX, then when view changes and log out button appears it's showing this error when clicked:
ActionController::InvalidAuthenticityToken in Customization::SessionsController#destroy
ActionController::InvalidAuthenticityToken
Parameters:
{"_method"=>"delete",
"authenticity_token"=>"hitX5aEjoTzi3tKP3y76+c60MuJumv5mwNEjyGUOQiY="}
Here you have my devise session controller's create method for signing in (I had to change few things to make AJAX happen):
# POST /resource/sign_in
def create
self.resource = warden.authenticate!(auth_options)
sign_in(resource_name, resource)
yield resource if block_given?
if user_signed_in?
flash[:logged] = "Zalogowany"
respond_to do |format|
format.html { redirect_to root_path }
format.js { render "sign_in_success.js.erb" }
end
end
end
sign_in_success.js.erb file:
$("body").empty();
$("body").append("<%= j(render template: 'layouts/user') %>");
button inside "layouts/user":
a href="#{destroy_user_session_path}" data-method="delete" rel="nofollow"
button.btn.btn-primary.m-r-20 type="button"
b Log Out
How do I send Authenticity Token with AJAX? Can I send it to "layouts/user" via locals option in render and use it this way?
I have solution. The problem was that csrf_meta_tags, was not updated when new session was created. The solution to that problem was to include additional code inside sign_in_success.js.erb file:
$("body").empty();
$("meta[name='csrf-token']").remove()
$("meta[name='csrf-param']").remove()
$("head").append("<meta name='csrf-token' content='<%= form_authenticity_token %>'>");
$("head").append("<meta name='csrf-param' content='<%= request_forgery_protection_token %>'>");
$("body").append("<%= j(render template: 'user/logged_signed') %>");
With code seen above firstly we are empting body element , then we are removing from DOM meta tags created by csrf_meta_tags helper. After that we are manually adding new csrf-token and csrf-param, this time our new authenticity token is assigned to meta name="csrf-token".
After that when we click on our log_out button rendered by "user/logged_signed" template we are logged out successfully and back at our root_path.
This may end up being very simple but I have been trying to figure this out to no avail. I am using wicked_pdf to render pdfs on html pages. When I do this the restful way by adding the respond_to block to a controllers show action everything works fine when going to controller/id.pdf.
What I am trying to do however, is use the respond_to block on a custom controller action. I have a reports controller which has many reports and custom actions. One of these reports has a form which is submitted to another custom controller action to render the report. This all works fine with the html render however, when I added the respond_to block to this custom action it simply tells me it cannot find the show action for that controller. Below is the related code:
routes.rb
resources :reports do
collection do
get 'customer_summary'
post 'summary_report'
end
end
reports_controller.rb
def summary_report
#tickets = tickets
respond_to do |format|
format.html
format.pdf do
render pdf: "summary_report", header: {spacing: 10, html: {template: 'shared/header'}}, footer: {html: {template: 'shared/footer'}}, margin: { top: 20, bottom: 20 }
end
end
The templates exsist as well as the summary_report.pdf.haml file. What am I doing wrong? Thank you in advance for your help!
Below you will see my jquery:
$(function() {
$(".pagination span.page a").click(function (){
$.get(this.href, null, alert("The pagination link was clicked" + this.href), "script");
return false;
});
});
I know my call on my classes are working because the alert pops up. I think that it is where I am calling "script" where it is failing because it is not pulling up the page. This is a Rails 3 application that is calling an index.js.erb script. Here is the index.js.erb:
$("#search").html("<%= escape_javascript(render("search")) %>");
Any ideas?
Are you trying to render a partial?
$("#search").html("<%= escape_javascript(render :partial => "search") %>");
When looking on how rails calls ajax I came across a post. It said to do the following:
def index
format.js
end
Once I called this it started working.
The code below is not working for some reason. As I click at "test" it redirects me to /home/test and shows nothing.
view/home/index.html.erb
<%= link_to "test", { :controller => "home", :action => "test" }, :remote => true %>
<div id='test_div'>
</div>
view/home/index.js.erb
$("#test_div").html("some text");
controller
class HomeController < ApplicationController
def test
"test123 test456"
respond_to() do |format|
format.js {render :layout => false}
end
end
end
What should I do to refresh div_test using ajax?
What does your routes file look like? Can you post it on here? Do a rake routes and post the output.
Here are some things that may help you:
Rails documentation for routing
Also, check out the Ajax on Rails Documentation and the section on link_to_remote. The link_to_remote function is not in Rails. What version of rails are you on? Bash: $ rails -v
Look at this answer on S.O. regarding AJAX on Rails calls.
Edit: Here is another answer regarding this that may help.
Another answer on SO for updating existing element
Edit: looking over this again, I dont think you need () after respond_to or the {...} in link_to. Look at the third link. I think you need to put the page in a partial and render it. Its not rendering right now since its render :layout => false
I'm trying to make a form in Rails that will respond with a js file. Right now, I have a file in app/assets/javascripts/login.js.coffee.erb that I'd like to be returned when the user submits the form via ajax (I've got users without javascript enabled working fine). Here's my template code for the form:
<%= form_tag("/trade/submit", :method => "post", :remote => true) do %>
# some stuff in here
<% end %>
In my trade controller, I have a method submit, which follows:
def submit
respond_to do |format|
format.html { render :layout => 'widget' + #widget_type.to_s, :template => 'login/index' }
format.js { render :action => 'login', :content_type => 'text/javascript' }
end
end
My respond with html works fine, but when calling the form via ajax, it returns this response:
Missing template trade/submit, application/submit with {:handlers=>[:erb, :builder, :coffee], :formats=>[:js, :html], :locale=>[:en, :en]}. Searched in:
* "~/app_dir/app/views"
Obviously, it shouldn't be looking in views, but rather in javascripts, right? I tried removing the render block after format.js (keeping it default so it will look for submit), and I get the same problem. Does this mean I have to save my js files in my views directory? Seems kinda messy, so I feel like surely I must just be doing something wrong. Any help is greatly appreciated!
It tries to find app/view/trade/submit.js.erb! Try to do a simple test.
touch app/view/trade/submit.js.erb
vim app/view/trade/submit.js.erb
alert('done!');
And run it again.