Rails: Select helpers are sending get request - ruby-on-rails

i'm currently getting an issue with this simple code :
=form_tag payments_path, :method => :post, :id => "braintree-payment-form" do
= select_month(14, :use_month_numbers => true)
= select_year(Time.now.year, :start_year => Time.now.year, :end_year => Time.now.year + 10)
In fact, when i select a value in one of the two selects above, it automatically to a get request on the current URL with the parameter i selected
exemple: if i click on 2013 in select_year it lead to /payments/2013
Anyone have a clue about it? It's exetremely annoying, i ran through this issue previouly and couldn't have found a proper solution.
Thanks!
UPDATE:
When i put these two selects out of the form tag, il also redirect me to /payment/selected_value, i really don't understand why it does that?
I tried put a e.preventDefault() in JS on the change of the selects but it has no effect

Related

Correct HAML for linking an image to another page in my Ruby on Rails app

Trying to figure out how to use haml "link_to" with Ruby code in order to make an image on a page link to another page on the site. If possible I'd like to keep the nav static and fade the two pages in and out via a "Forward" and "Back" image. Any ideas? Just want to get the linking right first and then can go in and figure out the JQuery. Currently have the code below...
Thanks!!
.pad-bottom50
%div{:style => "position: absolute; top: 620px; left: 830px;"}
=link_to (image_tag(#page.photos[1].image_url(:full), :id => "#fade1", :class => "animated") if #page.photos[1].image?
.pad-top20
Syntax of link_to is
=link_to link_text, link_url, options
You missed the link_url. ie. to where the user should be taken when clicking on the image.
Here is a working example
=link_to(image_tag("http://goo.gl/FZUI3"), "http://en.wikipedia.org/wiki/Nyan_Cat", :id => "#fade1", :class => "animated")
You are not specifying the src for the link.
The syntax is:
link_to "Link Text", "/path-to-link"
To put an image in there:
image_tag "path-to-image"
link_to(image_tag("path-to-image"), "/path-to-link")
This code will make an image wrapped by a link pointing to /bacon if #page.photos[1].image?
link_to(image_tag(image_tag(#page.photos[1].image_url(:full), :id => "#fade1", :class => "animated")), "/bacon", :id => "bacon", :class => "bacon") if #page.photos[1].image?
You should first look at the parameters of a link_to tag . You will get to know about the parameters and what you can do with those parameters .
check this link
api doc
You can check all paramters of a method avilable in API Ruby on Rails

simple posts list with buttons

Please help me to understand one dumb thing.
In one hand I have user with many posts, in other hand I have list with posts (every post have button) in view. On button I need to call custom method on current post with parameters. How to do this?
Try this:
link_to 'Submit', model_path(:query => "value"), :remote => true, :method => :post

haml and no javascript? (rails: form_remote_tag and :with parameter)

i am trying to call a remote method to update page content via ajax/js.
either i am too tired already or haml is not parsing the following code correctly to send the value of the query field via prototype. any ideas?
- form_remote_tag(:url => {:controller => "search", :action => "line"},:with => "'query=' + $('query').value" ) do
%input{:type => 'text', :id => 'query'}
%input{:type => 'submit', :value => 'Search'}
thanks a lot!
t
Have you tried a
= form_remote_tag
instead of
- form_remote_tag
I'm new to HAML myself but I was under the impression that you'll need the form tag to be actually generated not just executed...
Try passing the :with as part of the options hash.
- form_remote_tag({ :url => {:controller => "search", :action => "line"}, :with => "'query=' + $('query').value" }) do
If that doesn't work, debug the problem: Look at the generated html. Is the text field with id query the only element in the page with that id? Is the js code correct? Use the Firebug console to ensure $('query').value returns whatever you've entered into the text field.
Still stuck? Add your generated html into your question so we can better help.
EDIT: Your query input tag does not have a name attribute. Without a name, the javascript helper code skips that field when serializing the form fields...also, you do not need the :with code.
%input{:type => 'text', :id => 'query', :name => 'query'}

remote_form_for in rails with the :with option

Is it possible to use the :with option with remote_form_for?
It works with link_to_remote, and the api docs seems to indicate :with should work with any of the remote_ methods.
Here is the code I'm using, and output it's producing:
Link_to_remote (works):
= link_to_remote 'ARGH', {:url => {:action => 'load_item', :id => #policy.id} , :with => "'elephant=mouse'"}
arg
Remote_form_for (doesn't work)
= remote_form_for :policy, #policy, :url => {:action => 'load_item', :id => #policy.id} , :with => "'elephant=mouse'" do |f|
<form action="http://localhost:3000/quote_edit/load_item/5" method="post" onsubmit="new Ajax.Request('http://localhost:3000/quote_edit/load_item/5', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;">
Does anyone know how to make remote_form_for pick up on this option?
Or is it being ignored by remote_form_for as it needs to use parameters:Form.serialize(this) to pick up the values from the current form?
Cheers
Dave Smylie
I had the same problem when I wanted to use javascript to calculate the id of the url.
Researched the source code and determined that remote forms and submits don't seem to allow the :with statement to work.
I got around this using a hidden field called overridden_id on the form. Then in the :before option, I added javascript to set the hidden fields value.
Inside the controller I added "params[:overriden_id] || parmam[:id]" to my Find to override the default id passed into the url.

Reusing the partials multiple times on one page in Ruby on Rails

I have a partial that renders a select box using the following method:
<%= collection_select 'type', 'id', #types, "id", "name",
{:prompt => true},
{:onchange =>
remote_function(
:loading => "Form.Element.disable('go_button')",
:url => '/sfc/criteria/services',
:with => "'type_id=' + encodeURIComponent(value) + '&use_wizard=#{use_wizard}'"),
:class => "hosp_select_buttons"
} %>
This partial gets used 2 times on every page, but at one point I need to get the value of the first select box. Using:
$('type_id')
returns the second select box. Is there a way to find the first one easily? Should I fix this using javascript or by redoing my partial?
Note: the dropdowns do get rendered in separate forms.
Yes, each element does need a unique ID, the page probably also fails HTML validation. Also, unless these are in 2 different forms you'll have a conflict with the CGI parameter name.
If they are in 2 different forms you can probably get away with just setting the :id as you posted, if they are the same form you need to abstract the parameter name too:
collection_select 'type', "id_wizard_#{use_wizard}"...
I figured out one way to do this by assigning an ID in the html_options block. I already am passing in a value for use_wizard, so I can append that value on to the ID to differentiate between the two dropdowns.
:id => "type_id_wizard_#{use_wizard}"

Resources