Ruby On Rails: How to pass an Input value from a form to a controller in a link_to tag? - ruby-on-rails

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.

Related

Getting Globalize and autocomplete to work together

Using globalize gem to manage translations with autocomplete, there is a situation where a number of hooks need to be properly set. Note: this does not use hstore AFAIK. I have not managed to find a way to do so. The most productive set-up to date has
controller:
autocomplete :nation, :name, :full => true
Nation
translates :name
view
<%= autocomplete_field_tag 'nation_name', '', autocomplete_nation_name_itins_path, size: 35, :id_element => 'nation_id' %>
There is no inherent reference to nation_translations database table created by Globalize as of yet. As this image suggests, there is a problem:
Issue 1: The input remains binded to the base table's attribute value (I have not yet cleared them out as the Globalize gem suggests. Otherwise I'd be getting blanks). can is actually ready all values of canin master table... Typing in other locales, like cyrillic say Канада has naturally no effect as that value is not part of the Nation table.
What is interesting is that the drop-down values are being populated by Rails automatically, extracting the translation values of what is input.
Issue 2: I'd rather pass the parameter 'nation_id' which is naturally part of the nation_translations table with the form data. although I can append , :extra_data => [:nation_id] to the controller it is not being submitted (example in cyrillic where the input is given without any autocomplete)
{"utf8"=>"✓", "nation_name"=>"Канада", "commit"=>"..."}
Rails.logger.info :extra_data returns:
extra_data
Now the second issue can be overcome because a query like
Nation::Translation.where('name = ?', "Канада").pluck('nation_id')
returns a proper result. But that point is moot if the autocomplete is not playing ball with the user's input.
How can this be configured to have user input autocomplete with the current local translations?
this does get solved by creating an entirely new class with attributes nation_id, name, locale and can thus be called symbolically.
The query call is not that straightforward however. As the gem suggests, the method need to be tweaked
def autocomplete_nation_name
term = params[:term].downcase
locale = params[:locale]
nationtranslations = Nationtranslation.where('locale = ? AND name LIKE ?', locale, "%#{term}%").order(:name).all
render :json => nationtranslations.map { |nationtranslation| {:id => nationtranslation.id, :label => nationtranslation.name, :value => nationetranslation.name} }
end
intervening on the action method itself provides all the leeway desired...

How do I display the current value in an edit form using haml custom inputs?

The code I'm working with is dynamically rendering the input fields in this edit form, like so:
%input.custom{:name => "short_form[#{field_name}]", :type => field_type, :id => field_id, :value => short_form.send(field_name)}
Based on the field_type value it renders a certain type of input field. However, I cannot get the value to display at all. The value attribute just ends up showing up as blank. Any suggestions? field_name is the attribute name btw, and short_form is the object (this is in a partial).
Do:
:value => short_form.object.send(field_name)

Rails form formatting

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.

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.

Is it possible to detect the key for the array within the Rails params hash?

I'm using a inherited controller for an abstracted class. I'm trying to avoid overriding actions within all of the descendant controllers. How could I find out the name of the key where the array of form values is living? For example:
The master class currently uses this to update rows
if #parent.update_attributes(params[:parent])
Every class that inherits from it has its own name that will not be recognized
<input type="text" value="" name="child[title]" id="child_title">
How can I dynamically detect the :name I need to use in the params[ ] for each form? I would settle for some form_for guidance for my partial that handles edit and new:
form_for #parent do |f|
I've tried what is below but I can't get it to do PUT. When I edit it makes a new row:
form_for #child, :as => :parent, :url => child_path do |f|
I had the name of the key all along in my instance variable. I just didn't know how to get it out and turn it into the symbol I needed for the params hash. Here is how I did it:
#element.update_attributes(params[#element.class.name.downcase.to_sym])
This way I can make a resource, have it inherit from a parent controller, use the default form setup, and still recycle the logic from the parent controller.
I didn't really undestand what you are asking about. So I beg your pardon if my answer is not relevant.
1.As I understood the first thing you are looking for is to how map the particular the key in params hash with the actual descendant controller which has made post/put request. You can do so by examining "controller_name" something like that:
keys = {
'ChildController1' => :child1,
'ChildController1' => :child2,
}
key = key[controller.controller_name]
#model.update_attributes(params[key])
2.If you need to perform a put request add :html => {:method => :put} as the last param of form_for method of FormBuilder helper (form_for #resource, :url => {resource_path(#resource)}, :html => {:method => :put}

Resources