I have a form with a checkboxes:
-form_tag filter_path(#page.permalink), :method => 'get' do |f|
-ftype.producers.each do |producer|
=check_box_tag "producers[]", producer.id, false
=label_tag producer.title
%br
=submit_tag 'Сортувати', :name => nil
When I send a request, it sends a hash params with an array of producers.Link then looks like that:
'/pages/:page_id/filter?producers[]=4&producers[]=5'
And I want to make it look that:
'/pages/:pages_id/filter?producers=4,5'
Please help
It shouldn't be a problem, since ?producers[]=4&producers[]=5 will be converted by the framework into params[:producers] array with value [4, 5].
Thus, you already have an array and you don't even have to parse anything.
But if your really want to submit two input values in one parameter, you'd have to employ some javascript. By default, if you have in html form two inputs with the same name, two independent values will be submitted (like in sample url you provided).
So, it's not a Rails question, it's html and javascript question.
Related
I have a form with input tag which takes a value from the user and next to that there is a link which should take that value and pass it to a method in a controller which generates an encrypted values based on that hash.
However params[:hash] which is my input does not pass input to controller. gives NULL. Please look in to my code and let me know where I am going wrong.
thanks
view:
= simple_form_for(#user, :html => {:class => 'user_form'}) do |f|
= f.input :hash, :input_html => {:class => 'span4'}
= link_to('Click to confirm key Encryption',confirm_encrypt_path(#user,:hash=>?)
controller:
def confirm_encrypt
check = params[:hash]
puts check # gives null value
MyModel.reset_authentication_pin!(current_user.id,params[:hash])
end
I Expect the puts check value should be the user entered value.
It's hard to tell what's happening here because it appears you've only included a portion of the view, if you could edit your question, that might help shed some light on what else is happening.
Three things that stand out to me are:
In your view you call the input :hash, but when you attempt to get the value you get params[:temp1] not params[:hash]
In the link_to you include the user, but you don't include any value for the :hash. Off the top of my head, I believe you just need to do params[:hash] to successfully get the :hash value through to the controller with the way you have it.
In your view you set up a simple_form_for, but it appears that you don't use a submit to get the :hash value in the controller. If you decided to go that route, you'd just have to make sure that the form is being directed to the proper controller.
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) %>
so, I've got a form inside a bootstrap modal.
when the modal loads, some javascript calls to the server to figure out what the current user has already chosen (this is an edit modal). once the server response returns, the form select sets itself to the values that the user has already chosen as is demonstrated in the below screenshot.
My problem occurs when the form is submitted. The params being sent to the server have the key 'room_object_id' that points to a single value. Naming issues of that key aside, I need that key to point to an array of values or a string or any structure that will hold multiple values and this is not the case, as demonstrated by the below screenshot.
My form is being generated by
<%= form_for :student, url: student_path, html: {class: 'form-horizontal sync'}, method: :put do |f| %>
<%= f.select(:room_object_id, options_for_select(#rooms.map {|room| [ room['name'], room['objectId'] ]}),{multiple: 'true', include_blank: ''} , {:class => 'select2-init form-control force-full-width', required: 'true', multiple: 'true', name: 'room_object_id'}) %>
<% end %>
I'm a bit unclear how select2 and the html form are working together and I have a feeling my problems are related to this. I'm also a bit unclear of the different usages of rails select, collection_select and options_for_select. I've looked at other SO posts on this topic but nothing pointed me in the right direction. Any pointers would be much appreciated.
Are you using the JavaScript required to run select2?
$(document).ready(function() {
$("#id_of_your_select_field").select2();
});
If you are, is your JavaScript console displaying any errors?
I've just had Submitting multiple forms in Rails answered which led to another problem. In my form I have the following (there's quite a bit more):
= hidden_field_tag :event_id, :value => #event.id
.control-group
= label_tag :title
.controls
= select(:registration, "registrations[][title]", Registration::TITLE)
and the last line returns:
"registrations"=>[{"title"=>{"registration"=>"Mr"},
as opposed to the expected:
"title"=>"Mr"
I've tried:
= select(:registration, "registrations[][title]", Registration::TITLE)
which returns:
undefined method `registrations[][title]' for #
and also tried:
= select("registrations[][title]", Registration::TITLE)
which returns:
wrong number of arguments (2 for 3)
Look at the parameters below, event(_id) is only there once then the :title oddness starts, any idea what the problem may be?
{"utf8"=>"✓",
"authenticity_token"=>"BQXm5fngW27z/3Wxy9qEzu6D8/g9YQIfBL+mFKVplgE=",
"event_id"=>"7",
"registrations"=>[{"title"=>{"registration"=>"Mr"},
"first_name"=>"Name1",
"last_name"=>"Surname1",
"company_name"=>"Company1",
"designation"=>"Designation1",
"landline"=>"Landline1",
"cell"=>"Cell1",
"email"=>"address1#example.com",
"member"=>{"registration"=>"No"},
"dietary"=>{"registration"=>"None"},
"specify"=>"None"},
{"first_name"=>"Name2",
"last_name"=>"Surname2",
"company_name"=>"Company2",
"designation"=>"Designation2",
"landline"=>"Landline2",
"cell"=>"Cell2",
"email"=>"address2#example.com",
"member"=>{"registration"=>"No"},
"dietary"=>{"registration"=>"None"},
"specify"=>"None",
"title"=>{"registration"=>"Mr"}},
{"first_name"=>"Name3",
"last_name"=>"Surname3",
"company_name"=>"Company3",
"designation"=>"Designation3",
"landline"=>"Landline3",
"cell"=>"Cell3",
"email"=>"address3#example.com",
"member"=>{"registration"=>"No"},
"dietary"=>{"registration"=>"None"},
"specify"=>"None"}],
"commit"=>"Submit registrations"}
Please not that :dietary and :member are formated in the same way as :title. Thanks in advance for your assistance!
EDIT
So submitting to the hash via a text_field_tag is a simple is:
= text_field_tag "registrations[][first_name]"
But the problem comes in with my hidden_field_tag and select_tag.
It's adding bad values, for example:
"title"=>{"registrations"=>"Mr"}
and basically it seems I need to find a better way to add those values into the hash. I'll continue trying to find a solution and will post it here unless someone beats me to it.
Unless i'm reading it wrong, your first two select calls are the same. Have you tried = select(:registrations, "title", Registration::TITLE)? If you look at the documentation of the method in api.rubyonrails.org, it will state that the first value is the object, second is the property. That would be registrations => { :title => "Value" }, in the parameters. If you just want :title => "Value", then you need the select_tag method.
I'm using formtastic to collect information from a form and post dirctly to an external site.
I have no problem generating the form itself. However, since this is being submitted to an external site, they require that each input field have the specific IDs they specify, eg email or last_name -- not the closest Formtastic form, eg _email_input or _last_name_input.
I've looked at the Formtastic v1.2.3 code and I'm 90% sure the answer is "sorry, can't do that." I figured it couldn't hurt to check if I'm missing something. I would like some way to specify the ID completely, as in:
= semantic_form_for('', :url => "https://external_site.com/handler, :method => "post") do |form|
= form.input :last_name, :id => "last_name"
[etc]
Is this possible?
(I will note that I recognize that another, arguably superior approach would be to create an appropriate controller, sanity check the parameters locally, and dispatch the remote call from within the app only when it's well formed; however, that's not what I'm trying to do at the moment.)
Firstly i think you need to use semantic_fields_for for non-model forms. Next, to pass ids to each field, you can use the input_html options to specify them. for eg
form.input :email, :input_html => {:name => 'email', :id => 'email' }