Rails flash messages with HAML - ruby-on-rails-3

i started learn HAML: and i can't translate flash block to HAML:
<% flash.each do |key, value| %>
<div class="alert alert-<%= key %>">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong><%= value %></strong>
</div>
<% end %>

Here you go:
= flash.each do |key, value|
.alert{ :class => "alert-#{key}" }
%button.close{ :data => { :dismiss => "alert" } } x
%strong
= value
Just FYI you can add attributes to any element by attaching them as a hash after the declaration. If you don't specify an element, just a class or ID, HAML makes that element a div with the given class or id. But you could do this many ways. For instance, these are all the same:
%div{:class => 'foo bar', :id => 'test' }
.foo{:class => 'bar', :id => 'test'}
#test.bar{:class => 'foo'}
#test.foo.bar
All output: <div class="foo bar" id="test"></div>
You need to put computed attributes in the hash though, i.e.:
- klass = "bar"
%div{ :class => klass }
Outputs: <div class="bar"></div>
Also, note that in all the examples above, the :attribute => 'value' can be expressed as attribute: 'value', e.g.:
%button.close{ data: { dismiss: 'alert' } } x
Hope that helps.

Related

Passing JQuery Datepicker Data to Rails Controller

I'm new to Rails and JQuery so I'll try to explain this as best I can. I'm trying to pass my JQuery datepicker values to my rails controller. The page has data on it that I want the user to be able to filter based on a date range. There are a number of different posts on this subject:
Passing the variables from from jquery to rails controller
How to pass variables from AJAX form into controller?
I have tried to follow the guidance of these posts, but I'm still having trouble.
Here is my partial _dateFilter.html.erb =>
<%= form_tag({:controller => 'events', :action => 'dateFilter', :class => 'date_form'}, :remote => true) do %>
<%= datepicker_input "event", :start_date, :class => 'dateFilter', :dateFormat => 'mm/dd/y' %>
<%= datepicker_input "event", :end_date, :class => 'dateFilter', :dateFormat => 'mm/dd/y' %>
<%= submit_tag "Submit" %>
<% end %>
Here is the generated HTML =>
<form accept-charset="UTF-8" action="/events/dateFilter?class=date_form" data-remote="true" method="post">
<input class="dateFilter" id="event_start_date" name="event[start_date]" size="30" type="text" /><script type="text/javascript">
//<![CDATA[
jQuery(document).ready(function(){jQuery('#event_start_date').datepicker({"dateFormat":"mm/dd/y"})});
//]]>
</script>
<input class="dateFilter" id="event_end_date" name="event[end_date]" size="30" type="text" /><script type="text/javascript">
//<![CDATA[
jQuery(document).ready(function(){jQuery('#event_end_date').datepicker({"dateFormat":"mm/dd/y"})});
//]]>
</script>
<input name="commit" type="submit" value="Submit" />
</form>
Here is the dateFilter.js.erb =>
$('.dateFilter').append('<%= escape_javascript(render(#events)) %>');
Here is the events_controller =>
def dateFilter
#events = Event.find(:all, :conditions => ["EVENT_DATE_TIME_LOCAL BETWEEN ? AND ?", params[:start_date], params[:end_date]])
respond_to do |format|
format.html
format.json { render json: #events }
format.js
end
end
Finally here is the application.js =>
$(document).ready(function() {
$('#date_form').submit(function (){
$.ajax({
type: 'POST',
url: "/events/dateFilter",
beforeSend: function (xhr) {xhr.setRequestHeader("Accept", "text/javascript");},
data: { 'start_date' : $("input[name='event[start_date]']").datepicker(), 'end_date' : $("input[name='event[end_date]']").datepicker()}
success: function(data) { <%= render "events/dateFilter" %>}
});
});
This is log data from the server =>
Started POST "/events/dateFilter?class=date_form" for 127.0.0.1 at 2012-12-12 15:09:39 -0500
Processing by EventsController#dateFilter as JS
Parameters: {"utf8"=>"✓", "authenticity_token"=>"J/HU+DxNmIwEhu3keN071k6MGCCD/n/1UhXUD0MkH1Q=", "event"=>{"start_date"=>"12/06/12", "end_date"=>"12/13/12"}, "commit"=>"Submit", "class"=>"date_form"}
Event Load (1.7ms) SELECT "TBLEVENT".* FROM "TBLEVENT" WHERE (EVENT_DATE_TIME_LOCAL BETWEEN NULL AND NULL)
Rendered collection (0.0ms)
Rendered events/dateFilter.js.erb (0.3ms)
Completed 200 OK in 8ms (Views: 3.4ms | ActiveRecord: 1.7ms)
One of the problems is that I keep getting "NULL" for the values of data I am trying to pass. I'm not even sure that I am going about this correctly, any help/feedback on this would be greatly appreciated. Thanks.
From your log data, this is in the Parameters:
"event"=>{"start_date"=>"12/06/12", "end_date"=>"12/13/12"}
which means start_date and end_date are in a Hash called event, so your controller should look like this:
def dateFilter
eventIn = params[:event]
#events = Event.find(:all, :conditions => ["EVENT_DATE_TIME_LOCAL BETWEEN ? AND ?", eventIn[:start_date], eventIn[:end_date]])
respond_to do |format|
format.html
format.json { render json: #events }
format.js
end
end
One other small thing - the form_tag class should be set like so:
<%= form_tag({:controller => 'events', :action => 'dateFilter'}, :class => 'date_form', :remote => true) do %>

Validation in twitter/bootstrap modal rails 3.2

I am trying to get validation to return in my modal using rails 3.2,twitter/bootstrap and formtastic, but I am at a loss as to how to do this.
Below is a quick sample of what my modal and form look like. In the model I require firstname and lastname. If the user hits 'submit' without putting in a firstname or lastname, it immediately takes the user to my actual registration page and notifies them of what is missing.
But I would like it to return to this modal and let the user know what is missing. How do I accomplish this?
:In my header, this is how I bring up the modal:
=link_to 'Register', "#new_registration_modal", :data => { :toggle => "modal",:backdrop => "static" }
#app/views/subscribers/registration/_new_modal.html.haml
#new_registration_modal.modal.hide.fade
.modal-header
Some Text
.modal-body
Some Text
=semantic_form_for(:subscriber, :as => resource_name, :url => registration_path(resource_name),:html => { :class => "form-inline" } ) do |f|
=f.input :email
=f.input :firstname
=f.input :lastname
=f.input :password
=f.input :password_confirmation
=f.submit
.modal-footer
Some Text
I have found a solution working for my side (using simple_form), maybe can help you:
I want to add a new client inside a modal from another controller.
The view of the modal controller:
<div id="client_modal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="client_modal_label" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="client_modal_label">Create new client</h3>
</div>
<div class="modal-body">
<%= simple_form_for Client.new, html: {"data-type" => :json}, remote: true do |f| %>
<div id="error_explanation" style="display:none;"></div>
<div class="form-inputs">
# the inputs
# ... and "closing" the modal
The client_controller.create:
def create
# save the data
if #client.save
respond_to do |format|
format.html { redirect_to anagrafe_index_path, :notice => "Client create!"}
format.json { render json: #client, status: :created, location: #client }
end
else
respond_to do |format|
format.html { render :action => 'new'}
format.json { render json: #client.errors.full_messages, status: :unprocessable_entity }
end
end
end
The js.coffee where the modal is raised:
$ ()->
$("form.new_client").on "ajax:success", (event, data, status, xhr) ->
$("form.new_client")[0].reset()
$('#client_modal').modal('hide')
$('#error_explanation').hide()
$("form.new_client").on "ajax:error", (event, xhr, status, error) ->
errors = jQuery.parseJSON(xhr.responseText)
errorcount = errors.length
$('#error_explanation').empty()
if errorcount > 1
$('#error_explanation').append('<div class="alert alert-error">The form has ' + errorcount + ' errors.</div>')
else
$('#error_explanation').append('<div class="alert alert-error">The form has 1 error.</div>')
$('#error_explanation').append('<ul>')
for e in errors
$('#error_explanation').append('<li>' + e + '</li>')
$('#error_explanation').append('</ul>')
$('#error_explanation').show()

jquery ui dialog appears bottom of page

quite random behavior, I'm calling on a modal dialog to show a partial inside.
I'm working with simple_form + bootstrap and jquery-ui.
the button that calls the function has this code in the view:
<p><%= link_to 'New Boots', new_boot_path, :class => 'btn btn-primary pull-right', :remote => true, :id => 'new_boot_link' %></p>
in my views/boots/new the code is this:
<div id="content">
<%= render :partial => 'form' %>
</div>
and in views/boots/_form the following
<%= simple_form_for(#boot, :html => { :class => 'form-vertical' }, :remote => true) do |f| %>
<fieldset>
<%= f.input :number %>
<%= f.input :size, :collection => boot_size_options %>
<%= f.input :condition, :collection => boot_condition_options %>
<%= f.input :brand , :collection => boot_brand_options %>
</fieldset>
<div class="form-actions">
<%= f.button :submit, :class => 'btn btn-primary' %>
<%= submit_tag 'Reset', :type => :reset, :class => "btn btn-danger" %>
</div>
<% end %>
In application.js i have the following:
$(document).ready(function() {
$('#new_boot_link').click(function(e) {
var url = $(this).attr('href');
$('#modal').dialog({
title: "New Boots",
draggable: true,
resizable: false,
modal: true,
width:'auto',
open: function() {
return $(this).load(url + ' #content');}
});
});
});
So the modal works as it should, but it appears at the bottom of the screen, however if i close it and click on the button again, it shows right in the middle as it should have done in the first place.
Is it css? I think maybe not, as otherwise it would continue showing up constantly in the same place... so i dont know if is the way I'm calling the function?
Suggestions are welcome, this is quite an annoying glitch!
Sorry about previous mistakes.
Unfortunately I can't reproduce your problem in person. I suspect that this would help because it will convert the DIV and on call fill and display.
$(document).ready(function(){
var modal=$('#modal');
$('#new_boot_link').click(function(e) {
var url = $(this).attr('href');
modal.load(url + ' #content',function(){
modal.dialog("open");
});
});
modal.dialog({ autoOpen: false, title: "New Boots", draggable: true,
resizable: false, modal: true, width:'auto'});
});
Include: jQuery Center
Change to:
$(document).ready(function() {
$('#new_boot_link').click(function(e) {
var url = $(this).attr('href');
$('#modal').dialog({
title: "New Boots",
draggable: true,
resizable: false,
modal: true,
width:'auto',
open: function() {
return $(this).load(url + ' #content');}
});
}).center(false);
});
This set your dialog on middle-center of page.
If you have specific place you want show dialog force:
..}).css({position:"relative"}).css("left",null).css("top",null);
// Last too remove value of left and top. Trick with {left:null,top:null} does not work!

Trouble with locals and partials with Rails

I have the following in my js.erb file:
$('#menu').after($("<%=escape_javascript(render 'boards/customize', :board => #board, :templates => #templates, :types => #types)%>")
So I try to pass some locals to my partial
In my _customize.html.erb
<div id="customize">
<ul id="categories">
<% #types.each do |type|%>
<li><%=link_to type.name, change_type_board_path(board, :type_id => type.id), :remote => true %></li>
<% end %>
</ul>
<div id='carousel'>
<%=render 'boards/carousel', :templates => templates %>
</div>
</div>
I get the following error:
undefined local variable or methodboard' for #<#:0x00000103893e48`>
How are you supposed to pass in these variables to partials in Rails?
In Rails 3:
render :partial => 'boards/customize',
:locals => { :board => #board , :templates => #templates, :types => #types }

What is the Rails 3 Way to do a form field validation prior to submission?

I basically want to prevent searches that are less than 3 characters long.
In Rails 2.x, I would just do this in an onClick event. I understand how to handle AJAX requests and responses in Rails 3 now, but wouldn't using the onClick event be considered obtrusive in Rails 3?
Yup, onclick would indeed be considered obtrusive.
You might want to use a third-party validation library in javascript.
It could work something like this:
<form ... class="validated">
<input type="text" name="name" data-validation="presence=true,format=/[a-zA-Z0-9\s]+/" />
<input type="submit" value="Send" />
</form>
<script language="javascript">
$('form.validated').live('submit', function() {
var data = $(this).serializeArray();
var errors = [];
$(this).find('input').each(function() {
var input = $(this);
$.each($(this).attr('data-validation').split(','), function(validation) {
var type, param = validation.split("=");
if(type == "presence") {
if(input.val() == "") errors += input.attr('name') + " is empty";
} else if(type == "format") {
...
}
});
return (errors.length == 0);
});
</script>
(Just some rough code out of my head. Lots of caveats there.)
But instead of writing all that, perhaps you just want to use: http://github.com/jzaefferer/jquery-validation
Got it!
I put this code in application.js:
// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
$(function() {
$("#search_submit").click(function() {
if ($("#search_value").val().length < 3) {
alert("You must supply 3 or more characters in your search.");
return false;
}
})
})
My form is:
<%= form_for(#search, { :remote => true, :html => { :id => 'left', :class => "custom-border" } }) do |f| %>
<h2><%= f.label :value, "Search By Part or Interchange #" %></h2>
<p><%= f.text_field :value %></p>
<p class="button"><%= f.submit "Search" %></p>
<% end %>
Voilá!