Remote Method Understanding? - ruby

In rails 3
While using remote method form submit, it affects the database twice with the same form values.
controller file:
#user_message = UserMessage.new(params[:user_message])
if #user_message.save
render :update
else
render :nothing => true
end
view file:
<%= form_tag ('/feedback/user_message'), :method =>'post', :remote=> true, :id=>'user_message' do%>
<%= hidden_field_tag 'user_message[user_id]', #user.user_id %>
<h2><%= #question %></h2>
<%= text_area_tag 'user_message[msg]',"", :size=>"40x5" %>
<%= submit_tag "Submit"%>
<% end %>
When I hit the submit button it creates two records on the table.
Why?

There may be couple of things that might be causing this
may be the ujs file is included twice may may be like
//= require jquery_ujs // expected to load from the rails-jquery gem
//= require_tree . // if any file is present in assets directory hierarchy it will be loaded
Or just a bug taking your sleep away
https://github.com/rails/jquery-ujs/issues/208
check out more solutions
Jquery Rails 3... form submits twice... deletes twice... help
Rails 3.1 remote requests submitting twice
This may also be related to the understanding of asset pipeline when and how use pecompiled assets.
I always use this config in development mode
config.server_static_assets = false
this forces the app to call the assets from app assets
and use precompiled assets from public in production mode

The problem most likely related to asset pipeline
You should precompiled the asset pipeline before.
This will create two copy of rails.js, one in your assets and one in application.js
This is a bug or gotcha in rails 3
See here http://www.ruby.code-experiments.com/blog/2011/10/another-gotcha-with-the-rails-31-asset-pipeline-or-why-are-my-jquery-ujs-ajax-requests-triggered-twi.html
Hope this help

Related

Use rspec with capybara-webkit or selenium to measure page load times

I'm hoping to find a way to write rspec scripts (or something else) that can be used to measure page load times. So similar to how firebug can tell when a page has fully loaded, is there a similar way to do that in rspec?
Or even any other ruby gem?
Thanks!
You can use benchmark in your views:
<% benchmark("Showing projects partial") do %>
<%= yield %>
<% end %>
If you do this in your application layout you'll have a log with page loads:
Showing projects partial (0.0ms)

Rails, Twitter Bootstrap, Simple Form, Heroku - Odd error when using ":wrapper => :prepend do" to get an add-on span

I have the following code in a form partial:
<%= f.association :client %>
<%= f.input :url, :wrapper => :prepend do %>
<%= content_tag :span, "http://www.", :class => "add-on" %>
<%= f.input_field :url, :class => 'span4' %>
<% end %>
Trying to load anything to do with this form partial e.g. new/edit etc causes Heroku to error. It works flawlessly on localhost.
The only obvious error in the Heroku logs is:
ActionView::Template::Error (undefined method `source' for nil:NilClass):
I'm afraid I don't really understand that but thought it might help anyone trying to answer this.
If I change the form partial code it works on Heroku no problem at all e.g.
<%= f.input :url, :input_html => {:class => 'span4' } %>
Obviously I lose my nice bootstrap add-on span so I'd rather find out what the issue is rather than running around it.
Shot in the dark, but maybe an initializer that sets up the prepend wrapper (like initializers/simple_form.rb or initializers/simple_form_bootstrap.rb) is not being run by Heroku, either because it's not checked into git, or because of some environment-specific logic in the initializers or environment files.
Well after testing and some help from Dan I finally got to the bottom of it. Unfortunately, it seems to be quite localised to my situation though.
But just in case anyone stumbles across this, the error I had was the following:
ActionView::Template::Error (undefined method `source' for nil:NilClass):
That was in the log and was THE SAME when I followed Dan's advise to turn on error reporting on the production site (set consider_all_requests_local = true in config/environments/production.rb).
I thought that the error was being caused in the template, however, it was some validation rules in the model. The validation rules for me where some regex to stop the user starting the input with a certain string. They worked perfectly fine on my local, but Heroku really wasn't liking it!
I removed those validation rules and voila, my nice bootstrap add-on span was working perfectly.

Rails 3.1 Devise How To Change Flash Message CSS From notice to success?

Rails 3.1 and Devise 1.5 question.
I'm using the following code to display flash messages in my layout:
<% flash.each do |key, message| %>
<%= content_tag(:div, message, :class => "flash #{key}") %>
<% end %>
I'd like to change the css class for some of my confirmation messages from notice to success, but I don't know where to override or change the key because I don't know where it's set.
Can anybody point me in the right direction?
Thanks!
So the way to do this is to edit the devise controllers.
When you install Devise through the normal installation I don't think it install the controllers (at least it didn't for me).
So first you should manually add the controller files and put them into your project in the same location: https://github.com/plataformatec/devise/tree/master/app/controllers/devise
Once you have the files in your project, go to the "sessions_controller.rb" file.
One lines 16 & 25, you should see the code:
set_flash_message :notice, :signed_in
and
set_flash_message :notice, :signed_out
You just need to change ":notice" to ":success"
Hope that works!
I ran into this problem because I'm using the bootstrap-sass gem which uses a whole different class structure for flash messages. I took the route of solving this with sass rather than messing with devise at all (sass inheritance is awesome:).
So, I render the flash messages in app/view/layouts/application.html.haml:
%div.container
- flash.each do |key, value|
= content_tag(:div, value, class: "alert alert-dismisable alert-#{key}")
And, I add a simple rule to app/assets/stylesheets/custom.css.scss to get Devise's flash messages working:
/* Style Devise Flash messages like Bootstrap */
.alert-alert {
#extend .alert-warning;
}
That's it! now flash[:alert] is styled just like bootstrap's flash[:warning].
The css class is being set by the :class argument. To add a class of notice (or success, etc.), just change your call to the following:
<%= content_tag(:div, message, :class => "flash #{key} notice") %>

ArgumentError (2 for 1):Rails3.1.0 <%= stylesheet_link_tag "application" %>

wrong number of arguments (2 for 1)
Extracted source (around line #5):
<html>
<head>
<title>Assets</title>
<%= stylesheet_link_tag 'application'%>
<%= javascript_include_tag %>
<%= csrf_meta_tags %>
</head>
I've tried:
<%= stylesheet_link_tag :all %>
but I got:
<link href='assets/all.css' ...>
What confused me most is that I run an app on my MAC OS with almost the same config except mysql&Gemfile and it works well, but when I turn to centos5 I got these errors!
I had this problem too, and it turned out to be due to some code still targeting an older rails version, before the new asset pipeline work. Specifically, I had a redmine plugin using the method image_path with two arguments:
image_path('database_refresh.png', :plugin => 'redmine_rate')
This was no longer possible, and needed to be replaced with code that explicitly calculated the plugin specific path. I used code based on the wiki document at http://www.redmine.org/boards/3/topics/31445#Links-and-paths-to-plugin-assets
Note that this solution was for a problem I had with redmine, not rails, but could in fact be relevant.
<%= stylesheet_link_tag "application" %>
Should be fine. Rails has an assets pipeline to merges all the different stylesheets into one for the productive environment. I think it is an error in the assets pipeline.
Here is a good tutorial about this pipeline:
http://railscasts.com/episodes/279-understanding-the-asset-pipeline
To possibly fix your error have a look at config/environments/production.rb and try to set this to true
config.assets.compile = true
Another problem might be the configuration in the application.css have you perhaps deleted the comments or added something?
/*
* This is a manifest file that'll automatically include all the stylesheets available in this directory
* and any sub-directories. You're free to add application-wide styles to this file and they'll appear at
* the top of the compiled file, but it's generally better to create a new file per style scope.
*= require_self
*= require_tree .
*/
But these are just guesses... but I hope this leads you towards the solution of the problem.

How to create 1 view for 2 layouts in erb?

I make a project on serve and use erb syntax.
I want to compile two files: index.html (compiled with template content) and load.html (only content). I create two layouts (_clear.html.erb (with the only yield) and _default.html.erb) and create a partial (_content_for_page.html.erb).
There are problems with compilation of index.html.erb.
According to Rails manual (3.4.3 Partial Layouts) I have to write index.html.erb:
<%= render :partial => "_content_for_page.html.erb", :layout => "_default.html.erb" %>
But it doesn't work. Only partial is compiled.
What is wrong?
First I just want to point out Serve does not utilize Rails unless the rails gem is included in the Gemfile of your project, but that will do something unexpected considering Serve organizes your project differently than Rails.
Now I don't know if this answers your question, but it may help to know partials should be rendered without the beginning underscore and are usually passed as a symbol.
<%= render :partial => :content_for_page, :layout => :default %>

Resources