Gem Seer and Ajax on Ruby on Rails - ruby-on-rails

I am trying to display a Line graph using Seer using Ajax.
But all I get is a blank page when I make an ajax request.The same partial works fine when I display is using a render :partial.
Here is the partial _show.html.erb
<div id="chart"></div>
<%= Seer::init_visualization -%>
<%= Seer::visualize(
sensors,
:as => :line_chart,
:in_element => 'chart',
:series => {
:series_label => 'location',
:data_label => 'id',
:data_method => 'value',
:data_series => series
},
:chart_options => {
:height => 250,
:width => 500,
:axis_font_size => 12,
:colors => ['#0099CC','#990000','#009900'],
:title => "Rain Data",
:point_size => 0,
:line_size => 3,
:title_y => "Water Level in Feet",
:smooth_line => "true",
}
)
-%>
This works:
<%= render :partial => 'hcfcdsensors/show', :locals => {:sensors => #sensors , :series => #series} %>
But this Doesn't:
<%= link_to_remote "show" , :url => show_graph_hcfcd_url(#hcfcdsensors) ,:update => "graphDiv" %>
The complete web page just goes blank.
I have checked the action and availability of required variables in the page and its all there fine.
Any help is welcome.
Thanks
Shaunak

When making ajax call, You need to draw the chart manually.
Seer user " drawChart()" method to draw the chart.
Below is my ajax response
<% result_script = Seer::visualize(
#flight_result_search.sell_classes_for_graph,
:as => :area_chart,
:in_element => 'historical_sellout_chart',
:series => {
:series_label => 'name',
:data_label => 'days_prior',
:data_method => 'capacity',
:data_series => [#flight_result_search.current_sell_for_graph,#flight_result_search.historical_sell_for_graph]
},
:chart_options => {
:height => 400,
:width => 600,
:axis_font_size => 11,
:title => "Historical Sell Out Chart",
:point_size => 3,
:color =>['#324F69','#90000B']
}
)
%>
<%= result_script.gsub("</script>",' drawChart();</script>') %>
Don't forget to eval your ajax response as it is javascript code .
Also drawChart() should be in same script tag otherwise it doesn't work.

Couple of questions... what happens if you move <%= Seer::init_visualization -%> to the enclosing page rather than the partial? Also, check to see if there are any JavaScript errors when you refresh the page. In Safari or Firefox, open up the Console to check.

Related

Using Ajax with Rails link

I am using link_to tag to change the validity:
<%= link_to "Mark as " + (doc.is_valid ? "invalid" : "valid"),
:action =>'change_validity',:id => doc.id %>
Here, is_valid is a field in a table with boolean value. When it is true link will show
as "Mark as invalid". When I click the link it will call the method "change_validity" method
in controller. The method will toggle the is_valid field and show "Mark as valid" in view.
This one I want to do using AJAX. I tried to using link_to_remote. But I couldn't get it.
Can anyone explain how to do it???
Make one partial page.
_preview.html.erb and put below code into your partial view
<%= link_to_remote "Mark as " + (doc.is_valid ? "invalid" : "valid"), :update => "update", :url => { :action => "change_validity", :id => doc.id } %>
In your main view file.put below code
<div id="update">
<%= render :partial => "preview", :locals => { :doc => #doc} %>
</div>
In your controller should have below code
def change_validity
// do stuff here
render :partial => "preview", :locals => { :doc => #doc}, :content_type => 'text/html'
end
link_to_remote is not available in Rails 3. Add :remote => true to your link.
link_to "Mark as " + (doc.is_valid ? "invalid" : "valid"),
change_validity_path(:id => doc.id), :remote => true
EDIT: for rails < 3 try
link_to_remote(
"Mark as " + (doc.is_valid ? "invalid" : "valid"),
:url => {:action => "change_validity", :id => doc.id},
:update => "your_div_id",
:html => {:class => "something"}
)

Rails / Haml: How to create a post form?

I'm trying to make a simple form, but it's working not so fine.
This is my current form code:
%form{ :controller => 'tool', :action => 'activation', :method => 'post' }
%table{ :border => 0, :width => "100%", :height => "100%" }
%tr{ :align => "center", :valign => "center" }
%td
%input{ :type => "text", :name => "accountName" }
%input{ :type => "submit", :name => "submit", :value => "login" }
I am getting this url when trying to send data via form: 10.0.0.2:3000/activation.
I know that I can make route tool#activation to activation, but it's a wrong way, I want to sent post query to 10.0.0.2:3000/tool/activation, but :action => 'tool/activation' also is a bad way as far as I understand.
Can you give me advice ?
You should use the rails helper tags.
= form_tag tool_activation_path, :method => :post do
# The table
# The row
# The data
= text_field_tag "accountName", ""
= submit_tag "Submit"
See more here: http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html
Also, you should try to avoid unnecessary tables to style your layout. Instead, look to using CSS.

How to pass/retrive local var in rails 3.1.0 partial view?

Here is code in form.html.erb for partial view. Local var :sid was defined as the id of the record and it should be passed into the partial view standards.html.erb as a local var.
<% #rfq.standards.each do |r| %>
<p><%= render :partial => 'standards', :locals => { :f => f, :sid => r.id } %></p>
<% end %>
Here is the standards partial view:
<%= f.association :standards, :collection => Standard.active_std.all(:order => 'name'), :label_method => :name, :value_method => :id,
:prompt => "Choose std", :label => "standard:", :include_blank => true, :selected => :sid %>
When rendered, standards partial view should render a collection of standards with sid selected. However the code above does not select the standards at all. Tried sid (string) :selected => sid. This causes error saying that sid was not defined.
My question is what's the right way to pass and retrieve a local var in view? Thanks so much.
The problem was caused by the partial standards were not called in each and every place with exactly the same format. In one method (not shown here), the partial standards was called like: render :partial => standards, :locals => {:f => f}. After adding :sid => 0 as render :partial => standards, :locals => {:f => f, :sid => 0}, the sid here is passed in successfully.

Preseve line breaks from textarea in Rails controller submitted with submit_to_remote

I can't believe there isn't a standard way of doing this, but I'm submitting content from a textarea to a Rails controller, and it doesn't seem to preserve the line breaks (of any form).
Here is my view code:
f.text_area :keywords, :cols => 50, :rows => 10
submit_to_remote 'button', "#{t "add_keywords"}",
:html => {:id => 'add_keywords_button'},
:url => { :controller=> :keywords, :action => :add_to_site },
:escape => true,
:loading=>"Form.Element.disable('add_keyword_button')",
:complete=>"Form.Element.enable('add_keyword_button');",
:update => { :success => "keywords_table_decorator", :failure => "message"
After submitting this goes to a controller that just needs to be able to parse out each keyword, line by line. I've tried all of the variations on the following theme:
#keywords = params[:site_keywords][:keywords]
puts #keywords.gsub(/\n|\r|\r\n/,'*')
just to see if I can get something that I can do a further split with.
I'd appreciate advice on getting this to work.
Figured it out. I had this in my reset.css for all textareas:
white-space: normal;
Removing it made the problem go away.

Ruby on Rails: link_to_remote and rel

I want a link to remote to have a rel tag, because I want to use facebox with it.
I had it working with a regular link to... but I needed the link to remote to handle the event that a user doesn't have javascript enabled.
this, currently does't work (except for the non-javascript part )
<%= link_to_remote "Ask a Question",
{:url =>
{:action => :ask_question,
:id => #container.id.to_s,
:javascript_disabled => false
}, :rel => 'facebox'},
:href => url_for(
:controller => :view,
:action => :ask_question,
:id => #container.id.to_s,
:javascript_disabled => true) %>
In link_to_remote you pass in HTML options (like rel) in the third argument. In your code you're passing it in the second (i.e. the first hash). Try this instead:
<%= link_to_remote("Ask a Question",
{ :url => { :action => :ask_question,
:id => #container.id.to_s,
:javascript_disabled => false
}
},
{ :href => url_for( :controller => :view,
:action => :ask_question,
:id => #container.id.to_s,
:javascript_disabled => true ),
:rel => 'facebox'
}
)
%>
(As you know, some of the parentheses and curly braces are optional, but here I've included all of them for clarity, and probably would keep them in since you're passing a lot of complex arguments here.)

Resources