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.
Related
I am trying to add a URL parameter in a link_to block.
The code currently <%= link_to "Submit", :action => 'renderChart', :class => "btn", :remote => true, :params => params.merge(:limit => 5) %>
but this gives me an error.
It adds the :class and :action into the url parameter, not just the :limit. Why?
EDIT:
I add other URL params from another link that looks like this
link_to "Toggle Sort Direction",:action => 'renderChart', :remote => true, :params => {:sort => "desc"}
so when the user clicks the other link I want to add the limit to the url params and keep the sort params
Use this
<%= link_to "Submit",{ :action => 'renderChart', :remote => true, :limit => 5, :sort => "desc"}, :class => "btn" %>
Separate out the html_options: class is an html_option so pass it last.
Refer to link_to documentation.
UPDATE
As per the OP's concern in EDIT section of Question:
I add other URL params from another link that looks like this
link_to "Toggle Sort Direction",:action => 'renderChart', :remote => true, :params => {:sort => "desc"}
params :sort => "desc" are for Toggle Sort Direction link and they cannot be connected to the Submit link. When you click on a particular link, params specified in the link would be added to the params hash. So, if you need to pass :sort => "desc" as params upon clicking on Submit link then specify them explicitly as shown in my answer above.
I finally managed to get a solution myself.
If I very simply do this: :params => {:limit => ..., :sort => params[:sort]} i get exactly what I need. If there is a sort param it keeps it the way it is.
You need to explicitly separate the hashes:
<%= link_to "Submit", { :action => 'renderChart', :class => "btn", :remote => true }, params.merge(:limit => 5) %>
Take the link_to out and you have an implicit hash (key-value pairs) and Ruby is smart enough to know you want a hash:
:action => 'renderChart', :class => "btn", :remote => true, params.merge(:limit => 5)
But that last thing - that's not a key-value pair - it's a hash. So really, you have this:
{ :action => 'renderChart', :class => "btn", :remote => true, { ... } }
If you take Rails out of the mix:
{ x: 'value', {} }
And that's simply not a valid Hash :)
how to get selected/clicked value in calendar_date_select_tag in rails and pass it as params in controller (as format yyyy-mm-dd)as remote_function? I have tried like this
<%= calendar_date_select_tag "calendar", #time,
:embedded => true,
:onchange => remote_function(
:method => :get,
:url => {:action => "show", :id => #vehicle},
:with => "'mtg_date='+($(this).val()",
:loading => "$('date_spinner').setStyle({visibility: 'visible'});",
:complete => "$('date_spinner').setStyle({visibility: 'hidden'});" ) %>
But it seems to be not working
Write following in the environment.rb/application.rb depending on your rails version
CalendarDateSelect.format = :american
Ref this for other formats
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.
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.
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.)