RnR using params[] getting hash when I want value - ruby-on-rails

I have a web form to which I want to take a parameter from the URL and add it to the web form as a hidden field on the form.
<%= form_tag(:action => 'create') do |f| %>
<%= text_field_tag :email,"Your email address...", :class => "text", :id => "email", :name => 'email',
:onFocus => "change(this,'#222222'); this.value=''; this.onfocus=null;", :size => "26" %>
<%= hidden_field_tag :ref_code, :id => 'ref_code', :name => 'ref_code', :value => params[:ref_code] %>
<%= submit_tag "Enter To Win", :class => "button-positive submit" %>
<% end %>
If I just do a:
<%= params[:ref_code] %>
I get the value I want which is a five character alphanumeric, however when I use it in the form, I get the full hash description:
{:id=>"ref_code", :name=>"ref_code", :value=>["k53e5", "home", "index2"]}
Why? I tried .values, .to_s, and other ways of getting by key and I always get the full hash instead of just the value. What am I doing wrong? Thanks.

http://apidock.com/rails/ActionView/Helpers/FormTagHelper/hidden_field_tag
hidden_field_tag(name, value = nil, options = {})
<%= hidden_field_tag :ref_code, params[:ref_code], { :id => 'ref_code', :name => 'ref_code' } %>

Related

Rails remote form submitting duplicate form data

When I submit a remote form in rails, the form POST duplicate data, even though if I manually serialize the form using jQuery shows that there are no duplicates.
What could be causing this?
*Normally this wouldn't be a problem, but one of the form data is an array, as a result there are duplicate value in the submitted array.
Result from chrome's network inspector
Result from jQuery
UPDATE
Here is the gist of my form (removing the html markups):
<% #order.available_payment_methods.each do |method| %>
<%= radio_button_tag "order[payments_attributes][][payment_method_id]", method.id, method == #order.available_payment_methods.first %>
<%= Spree.t(method.name, :scope => :payment_methods, :default => method.name) %>
<%= render :partial => "spree/checkout/payment/#{method.method_type}", :locals => { :payment_method => method } %>
<% end %>
# partial
<% param_prefix = "payment_source[#{payment_method.id}]" %>
<%= label_tag "name_on_card_#{payment_method.id}", Spree.t(:name_on_card) %><span class="required">
<%= text_field_tag "#{param_prefix}[name]", "#{#order.billing_firstname} #{#order.billing_lastname}", { id: "name_on_card_#{payment_method.id}", class:'form-control'} %>
<% options_hash = Rails.env.production? ? {:autocomplete => 'off'} : {} %>
<%= text_field_tag "#{param_prefix}[number]", '', options_hash.merge(:id => 'card_number', :class => 'required cardNumber form-control', :size => 19, :maxlength => 19, :autocomplete => "off") %>
<%= text_field_tag "#{param_prefix}[expiry]", '', :id => 'card_expiry', :class => "required cardExpiry form-control", :placeholder => "MM / YY" %>
<%= text_field_tag "#{param_prefix}[verification_value]", '', options_hash.merge(:id => 'card_code', :class => 'required cardCode form-control', :size => 5) %>
<% if #order.bill_address %>
<%= fields_for "#{param_prefix}[address_attributes]", #order.bill_address do |f| %>
<%= render :partial => 'spree/address/form_hidden', :locals => { :form => f } %>
<% end %>
<% end %>
<%= hidden_field_tag "#{param_prefix}[cc_type]", '', :id => "cc_type", :class => 'ccType' %>
UPDATE Submitting the form without remote: true fixes the problem (BUT I NEED IT!)
Add onsubmit="return false();" on your form it will make sure that form is not submited through default form submission. It will be submitted through ajax as you are adding remote: true

prepopulate form with data in rails 2

I have a form with some fields in my rails 2 application and I want to prepopulate the value from what was entered before. The parameters values are in the url
here is the code for the form:
<% form_tag({:controller => "articles", :action => "search"}, :method => "get") do %>
<%= label_tag("start_date") %>
<%= text_field_tag("start_date","", :type => "date") %>
<%= text_field_tag "username", "", :placeholder => "username" %>
<%= text_field_tag "email", "", :type => "email" %>
<%= select_tag "status", options_for_select([["status", ""],"approved", "unchecked"])%>
<%= submit_tag("Search") %>
<% end %>
I have tried to use for instance #username = params[:username] in the controller but I still get an empty field
Problem is you're providing empty content for the field:
<%= text_field_tag "username", "", :placeholder => "username" %>
Rails doesn't automatically takes instance variables based on the field name, so if your controller defines a #username variable, you have to explicitly use it:
<%= text_field_tag "username", #username, :placeholder => "username" %>
I am not sure if it is working in Rails 2 but I would try to do something like this:
<%= text_field_tag "username", :placeholder => #user.name %>
Of course, in the controller action that renders this view you need to provide #user

Rails: dynamically changing form content

I am building message app in rails where user can send message from templates. I have a database of templates, and the user can select templates as per category.
In my message model, I want to render the template dynamically based on category selected. I looked for examples on google, but was not able to find a relevant solution.
This is my message form:
<%= form_for #message, :html => {:multipart => true} do |m| %>
<%= m.select :biz_case, options_for_select(Message::Bcase), :prompt => "Select business case" %>
<%= m.text_field :subject, :class => "message-text", :placeholder => "Subject" %>
<div class="message-body">
<%= m.text_area :message, :class => "message-body", :class => "redactor", :placeholder => "Your content" %>
</div>
<%= m.select :user_type, options_for_select(Customer::CType), :prompt => "Customer segment" %>
<%= m.submit %>
<% end %>
In the above form, I am looking to display the subject and body based on the selected business case. Something like:
if biz_case == "promote"
subject = #template.subject where ("biz_case = ?", "promote")
message = #template.content where ("biz_case = ?", "promote")
end
The subject and message would be displayed in input text fields.
Can anyone tell me how to do this?
in your method:
#subject = #template.subject where ("biz_case = ?", "promote")
in view:
<%= m.text_field :subject, :value => #subject, :class => "message-text", :placeholder => "Subject" %>

RoR set default value for text field while using observe

I am trying to set a default value for a text field used for a search, which uses the observe method. Here's what I got which works.
<% form_tag('javascript:void(0)') do %>
<%= text_field_tag 'phrase', nil, {:onfocus => "$('results').show();", :onblur => "$('results').hide();"} %>
<%= observe_field :phrase,
:frequency => 0.5,
:update => 'results',
:url => {:controller => :profiles, :action => 'search', :only_path => false },
:with => "'phrase=' + encodeURIComponent(value)" %>
<% end %>
This works fine, but obviously the value is nil.
Now if I add in the value like so:
<% form_tag('javascript:void(0)') do %>
<%= text_field_tag 'phrase', :value => 'test', {:onfocus => "$('results').show();", :onblur => "$('results').hide();"} %>
<%= observe_field :phrase,
:frequency => 0.5,
:update => 'results',
:url => {:controller => :profiles, :action => 'search', :only_path => false },
:with => "'phrase=' + encodeURIComponent(value)" %>
<% end %>
An exception is thrown.
Any idea on how I can get a default value for this text field?
Thank you.
Do not use :value => 'test', simply use 'test':
<%= text_field_tag 'phrase', 'test' %>
The format you tried to use is for text_field helper that is usually coming from a model, text_field_tag does not typically come from a model.

how to send a parameter not associated with the model object using form_for in rails

I use form_for to save a model object site
<% form_for :site :url => {:controller => 'site', :action => 'add_site' } do |f| -%>
<%= f.text_field :protocol, :size => 127, :style => 'width:255px' , :value => "http://"%>
<%= f.text_field :site_name, :size => 127, :style => 'width:255px' , :value => "www."%>
<%= f.hidden_field :user_id, :value => #user.id %>
<%= f.hidden_field :status, :value => 'Not Verified' %>
<% end -%>
here the field protocol is not a instance of the model site. but i just want to pass to the action add_site so that i can use as params[:protocol]
what should i do to achieve this?
You can set something like this:
<%= text_field_tag :protocol %>
To the action of the controller you can refer to this as params[:protocol]
Add it to your site model as an attribute accessor:
attr_accessor :protocol
See http://railscasts.com/episodes/16-virtual-attributes .

Resources