rails 3, any way to add a row identifier (or query param) to submit_tag? (how identify the row being submitted?) - ruby-on-rails

What is 'the rails way' to have a form with submit_tags for each row, where the submit_tag 'tells' the controller which row's submit was clicked?
Inside a form_tag, my form displays a list of users, and next to each user are several actions such as 'foo' and 'bar'.
Currently I use link_to for those actions, which adds a query param :row => this_row.guid so my controller knows which row to take action on. It works, but I don't like having the query params exposed on the url, and I don't like how they persist on the URL so if the user clicks refresh it performs the action again.
Since the rows are displayed are inside a form already, I'd like to have each action be a submit_tag. But I do not see from the API docs that there is any way to add a different query param to each instance of a submit_tag.
When I try this:
= submit_tag "foo", {:row => this_row.guid}
= submit_tag "bar", {:row => this_row.guid}
The html DOES look like <input .... row='SOMEGUID' ...>
However the POSTed params do not include the :row query params
I also tried
= submit_tag "foo", params.merge(:row => this_row.guid)
= submit_tag "bar", params.merge(:row => this_row.guid)
with the same result ( includes the row=GUID, but that param is not POSted to the controller when the user clicks the submit button).
I'd appreciate any help on how to have many rows of submit buttons (with the same name visible to the user) also pass a row identfier to the controller.

the value of the submit button also gets sent as a parameter. check your logs.
UPDATE
Add the row id as a data attribute of the submit button:
= submit_tag "bar", {:data => {:row => this_row.guid}}
Then when the submit button is clicked it should add the row id as a hidden field in the form before it gets submitted.
$('input[type="submit"]').click(function(){
$('form').append('<input type="hidden" name="row" value="' + $(this).data('row') + '" />');
});

Related

How to add a search parameter to an existing Ransack query

I have a search form on a page, that allows a user to filter a list on multiple conditions. Now I'd like to add quick links on top of the list to apply scopes. I tried it like this:
= link_to "Visited", q: {"activated" => true}
While this filters the list to show only activated items, it also resets the search query. In other words, it doesn't remember what was already filtered in the form.
Is there a way to adapt #q so that I can add this "activated" => true to the hash of required filters?
Assuming you're only using the :q param to filter, you could aggregate that.
= link_to "Visited", q: (params[:q] || {}).merge(activated: true)
I don't think you can because if you follow a link you are not submitting the form therefore the parameters are not going to be submitted.
Passing the params in your link to will send the params if any exist:
= link_to "Visited", q: {"activated" => true, "your_params" => params}
This will only work if the form has been submitted once though, otherwise the params would be empty.
EDIT
I assume that the fields on your forms are populating if there is a value.
For example,
<%= text_field_tag(:email, if !params["email"].nil? then params["ip_email"] end) %>

How to assign a variable the current output of a select tag

I'm having some trouble with a select tag.
I want to assign a value to a variable depending on which name is shown on the select tag:
<%= select('product', 'name' , #products.map { |s| [s.name, s.id] }, {}, {:class => "form-control"}) %>
The values display correctly, but I want to assign the currently selected value to a variable so that it can be used later on the same page to display content with the relevant details of the selected product.
For example:
#product = currently_displayed_name
Is this possible with Rails and how to do it? Or do I might need to use another resource?
Props to the comments, the answer is that Rails does not give you a way to do that. Depending on what you need to manipulate when user changes select value, you might be able to find a JavaScript library for it.
Otherwise, you can load the default variable on the page, and then handle the rest with an onchange handler:
$('#product_name').change(function() {
// perform actions on the page with javascript
});

How do I build link with specific part of params hash from previous request?

While I wouldn't normally create a page like this, please know this is a current constraint I can't change.
The page has a checkbox form as well as a table with links for THs that sort the table. I need to construct the TH link in a way that it retains the checkbox items already checked.
Checkbox constructed in View with Haml as:
= form_tag movies_path, :method => :get do
Include:
- #all_ratings.each do |rating|
= rating
= check_box_tag "ratings[#{rating}]", "1", (#ratingsarray.include?(rating) ? true : false)
= hidden_field_tag 'sort', #sort
= submit_tag 'Refresh'
Then for the table it has this for the TH
%th{:class => #classrelease_date}
%a#release_date_header= link_to "Release Date", movies_path(:sort=>'release_date', :params[:ratings]=>params[:ratings])
Ultimately I want the URL like "/moves/?sort=release_date&Ratings[PG]=1&Ratings[G]=1" where I am spitting out the ratings params back to the page as part of the URL. Or how to I pass the ratings params in any part of page where the existing controller code will read it.
Existing controller code access ratings from checkbox:
params[:ratings]
Since movies_path accepts hash as parameter, you can tailor params and then generate the URL with movies_path(params). Generally, you may need to remove "controller" and "action" from params.

rails form - 2 fields with same id in a form - how to disable second field when first field is selected

I have in my rails form the following code:
<label>Fruit: </label> <%= f.select(:IST_FRUIT, [['Apple', 'Apple'],
['Orange', 'Orange'],
['Kiwi', 'Kiwi'],
['Other', 'Other']
],{ :prompt => "Please select"},{:onchange => "if (this.value == 'Other')
{document.getElementById('otherTissue').style.display = 'block';
} "}
) %>
<span id="otherFruit" style="display:none;"> If other, please state: <%= f.text_field :IST_FRUIT, :size => 10 %></span>
User can select a fruit form the listbox but if user selects 'Other', a textfield is diaplayed allowing user to input his value.
The problem is that when user selects a fruit and save the form, the fruit field in blank in the table and the reason is that the form is saving the second field 'IST_FRUIT' found in the span with id 'IST_FRUIT'.
I would be really grateful if someone could show me a way how to disable the second field when 'other' is not selected and enable it when 'Other' is selected from the dropdown list.
Many many thanks for any suggestion provided.
First, be aware that rails posts are based on fields' name, not ids.
In your case, you should use a virtual attribute to store the potential other fruit value.
Your model could look like:
attr_accessor :other_fruit
before_save :check_fruits
def check_fruits
#if other_fruit is not nil, it means that you want to store it's value in you IST_FRUIT column
#BTW, why these capital letters?
IST_FRUIT = other_fruit unless other_fruit.nil?
end
And your form would be:
<span id="otherFruit" style="display:none;"> If other, please state: <%= f.text_field :other_fruit, :size => 10 %></span>
I have another advice. Instead of disabling a HTML input (by the way - an ID must be unique in a HTML document) just make another attribute in your model, name it 'other_fruit' for example, and use this value if the 'fruit' is set to 'other' or is empty. For example you may write something like this:
class TheModel
attr :other_fruit
# Overwritten accessor for the fruit attribute.
# Returns the values of :other_fruit if :fruit is blank.
#
def fruit
if self[:fruit].blank?
other_fruit
else
self[:fruit]
end
end
end
Now the HTML part. In your JavaScript you set 'display' to block, but you do not reset it when user selects another option.
If you want to prevent the field from being sent to the server, you should set the 'disabled' attribute. Setting the 'style' only hides the control from the user's eyes.

Rails: how to get value from another field when executing an onchange remote function in textfield

I want to create a form for making a reservation for borrowed items. A reservation consists of pick up time, return time and items the reserver wants to borrow and their amounts. For example, I might want to reserve three plates, three knives and three forks for the rest of the week.
In the form, I want to do an AJAX validation that checks whether there is enough items available. Next to each item I have a text box with which the reserver inputs the item amount. After the input, I want to do an onchange call that checks if the amount of items is available for the given dates. Thus, I need to pass the remote function called in the onchange following parameters: item id, item amount (value of the current textfield) and pick up time and return time which are both given in datetime_select fields above. This is my code:
<% with = "'amount='+value+'&item=#{item.id.to_s}&pick_up_time=#{#reservation.pick_up_time.to_s}&return_time=#{#reservation.return_time.to_s}'" %>
<%= text_field_tag "reservation[#{prefix}reserved_items][#{item.id}]", get_amount_value(item, #reservation), :size => 3, :onchange => "#{remote_function(:url => { :controller => :items, :action => :availability }, :with => with, :update => availability_url) }" %>
Obviously, this does not work since #reservation.return_time and #reservation.pick_up_time are not yet set because the form is not yet sent. My question is: how do I get those values? I believe that it should be done via a javascript call, but I didn't manage to insert a javascript call in the "with" variable or at least didn't get it to work. Does anybody have any idea on what I should do?
use prototype selectors $(#reservations_pick_up_time).value
the :with attribute is just a place for you to write JS code that it will display inline

Resources