Alert not working in Rails/ajax situation? - ajax

Ch8 of Beginning Rails book has an Ajax forms example which works ok except it does not output an alert for invalid input.
The controller code is:
def create
#comment = #article.comments.new(params[:comment])
if #comment.save
respond_to do |format|
format.html { redirect_to #article, :notice => 'Thanks for your comment' }
format.js
end
else
respond_to do |format|
format.html { redirect_to #article, :alert => 'Unable to add comment due to errors in your input'}
# logger.info("#{Time.now} Ajax invalid validation
##{#comment.errors.full_messages}!")
format.js { render 'fail.js.erb' }
end
end
end
The 'fail_create.js.erb' file contains the one line;
alert("<%= #comment.errors.full_messages.to_sentence %>");
Can some kind person explain why this is not working, thank you

You are rendering "fail.js.erb" but in your question you say the file "fail_create.js.erb" contains the code. Is that a typo in your question or is that the problem in your code?
Also, using RJS is considered bad form in Rails. If you are going through the example as a learning experience more power to you, but it was a failed experiment in the Rails community. Javascript should live on its own (and preferably be added to a site unobtrusively).

Book says to use jQuery 1.4.2
Upgraded to 1.4.4 and all worked well

Related

Render PDF on custom controller action using wicked_pdf

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!

Rails - trying to create simple ajax example

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

Rendering JSON in Rails 3.0

I'm having trouble rendering JSON with Rails 3.0. Whenever I visit the URL, nothing appears to show up on the screen (I'm pretty sure the JSON should be displaying kind of like XML?) Sorry, I'm fairly new to Rails in general
Here is my code.
def rjson
#comments = Chat.all
respond_to do |format|
format.json { render :json => #comments }
end
end
I simplified it as much as possible. Based on some of the tutorials I have
My routing looks like this:
match '/chatbox/rjson', :to => 'chatbox#rjson'
I'm pretty sure my model is fine.
I don't know if I should even have a 'rjson' view like (rjson.json.erb?) but I'm pretty sure I can just render from the controller without a view right?
If you are sure rjson will only response with json format, you just need:
def rjson
#comments = Chat.all
render :json => #comments
end

Can't Get Rails 3.1 to respond with js file correctly

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.

Form validation errors in Rails 3

I have a form I built using Formtastic in Rails 3. The form submits to the #create action of ClientsController, and if the save is successful the controller redirects to a "thank you" page. If the save is not successful, it redirects to the page where the form is embedded. I'd like to be able to show the validation errors inline on the form page, but after the redirect back to that page, the errors object comes back empty.
What can I do so I can display the validation errors on the form?
Here is the code from the controller.
if client.save && event.save
redirect_to "/thank-you"
else
redirect_to :back
end
Some additional details: I am using RefineryCMS so there is no view for the page where the form is, and therefore I can't render the view.
You need to save your errors into session.
if client.save && event.save
session[:client_create_errors] = nil
redirect_to "/thank-you"
else
session[:client_create_errors] = client.errors
redirect_to :back
end
now you can access errors object from anywere using session[:client_create_errors]
Why not:
render :action => 'name-of-the-view-the-form-is-in'
instead of the redirect_to :back ?

Resources