how to set selected value in <%= f.select_tag %> dropdown list, how? - forms

i have a long list dropdown menu including all currencies, i want the last selected value to be the default selected value in the list.
i am using Rails 4.0.0 and ruby 2.0.0
i am not using a model, i am just using a controller. i have put the dropdown list options inside a helper.
form.html.erb
<div class="calculator">
<%= form_for :convertor, :url => {:controller => "converter", :action => "show" } do |f| %>
<%= f.label :from_currency %>
<%= f.select :from_currency, options_for_select(currencies, :selected => params[:from_currency]) %>
<%= f.text_field :amount, :placeholder => "Amount", id: "textfield" %>
<%= #amount %>
<br>
<%= f.label :to_currency %>
<%= f.select :to_currency, options_for_select(currencies, :selected => params[:to_currency].to_i ) %>
<%= #result %>
<br>
<%= f.submit "Calculate", class: "btn btn-large btn-primary", id: "submitButton" %>
<% end %>
</div>
the list options are loaded from a helper *<%= f.select :to_currency, options_for_select(currencies, :selected => params[:to_currency].to_i ) %>* with name currencies
the dropdown list, in a helper
def currencies
[
['United Arab Emirates Dirham (AED)', 'AED'],
['Netherlands Antillean Guilder (ANG)', 'ANG'],
['Argentine Peso (ARS)', 'ARS'],
['Australian Dollar (A$)', 'AUD'],
['Bangladeshi Taka (BDT)', 'BDT'],
['Bulgarian Lev (BGN)', 'BGN'],
['Bahraini Dinar (BHD)', 'BHD'],
]
end
what am i doing wrong here?

A couple of things:
options_for_select takes two arguments: the list of options and the value that you want selected.
You're passing through the value as a hash: :selected => params...
The parameter name is wrong.
In your controller, you're saying that the parameter is called this:
params[:convertor][:from_currency]
But in your view, you have params[:from_currency].
Therefore, the solution to this is to do this:
<%= f.select :from_currency, options_for_select(currencies, params[:convertor][:from_currency]) %>

This code works for me.
in application helper
module ApplicationHelper
def opts_for_select_cur
opts_for_select = [
['United Arab Emirates Dirham (AED)', 'AED'],
['Netherlands Antillean Guilder (ANG)', 'ANG'],
['Argentine Peso (ARS)', 'ARS'],
['Australian Dollar (A$)', 'AUD'],
['Bangladeshi Taka (BDT)', 'BDT'],
['Bulgarian Lev (BGN)', 'BGN'],
['Bahraini Dinar (BHD)', 'BHD'],
]
return opts_for_select
end
end
and in view
<script type="text/javascript">
$(document).ready(function(){
$('#st').change(function(){
var inputText = $('#st :selected').val();
$("#hidden_one").val(inputText);
$("a").attr("href", "?value="+inputText );
});
$('#aa').click(function(){
var inputText = $('#st :selected').val();
$("a").attr("href", "?value="+inputText );
});
});
<%= f.select :text, options_for_select(opts_for_select_cur, params[:value]), {}, { id: "st" } %>
<br>
<%= link_to "refresh",nil, id: "aa" %>
the result is a select box with selected value.

this can help
<%= f.label :from_currency %>
<% if #from_cur.present? %>
<%= f.select :from_currency, options_for_select(currencies, #from_cur) %>
<% else %>
<%= f.select :from_currency, options_for_select(currencies), :required => true %>
<% end %>
<%= f.text_field :amount, :placeholder => "Amount", :required => true, id: "textfield" %>
<br>
<%= f.label :to_currency %>
<% if #from_cur.present? %>
<%= f.select :to_currency, options_for_select(currencies, #to_cur), :required => true %>
<% else %>
<%= f.select :to_currency, options_for_select(currencies), :required => true %>
<% end %>
i appreciate.

Related

Can you the coccon gem without a direct nested association?

I have to convert my form to not using nested association. In other words, instead of
<%= link_to_add_association f, :contacts, class: 'btn btn-primary', partial: 'projects/contact_fields', data: {
association_insertion_node: '.contact_fields', association_insertion_method: :append
} do %>
<i class="fas fa-plus"></i>
<% end %>
<%= f.fields_for :contacts do |contact| %>
<%= render 'projects/contact_fields', f: contact %>
<% end %>
I would like to be able to just pass in a string to be used as the container (similar to how you can with field_for).
<%= link_to_add_association 'contacts[]', 'projects/contact_fields', class: 'btn btn-primary', partial: 'projects/contact_fields', data: {
association_insertion_node: '.contact_fields', association_insertion_method: :append
} do %>
<i class="fas fa-plus"></i>
<% end %>
<% #contacts.each_with_index do |contact, index| %>
<%= fields_for "contacts[#{index}]", contact do |c| %>
<%= render 'projects/contact_fields', f: c %>
<% end %>
<% end %>
Cocoon is currently unable to edit/manage a collection. Cocoon is just helping the form-behaviour for nested-children functionality in rails, so there is no simple solution to edit an array or collection. On the other hand, this is in general pretty simple to implement without cocoon.
Very high level, do something like:
#contacts.each do |contact|
= render `contacts/edit`, contact: contact
= render `contacts/new`
so render the edit form for each existing contact, and render an empty new form. You will have to edit your controller functionality a little, because you will always re-render the complete collection/index page (with all existing contacts in the collection?).
So you can just render multiple forms on one page. Using turbolinks this will render fast and feel actually completely the same. You could use xhr to update only specific parts of the page, but to get started that is not even needed.
So I ended up following drifting ruby and using code from the cocoon gem to implement something myself. I hope others can benefit from it. Thank you nathanvda for the cocoon gem which helped me with the code below, really wish I could have used it:
add this to your app/helpers/application_helper.rb
def link_to_add_row(*args, &block)
if block_given?
link_to_add_row(capture(&block), *args)
else
#byebug
name, association, new_object, partial, html_options = *args
html_options ||= {}
html_options[:class] = [html_options[:class], "custom_add_fields"].compact.join(' ')
id = 'NEW_RECORD'
fields = fields_for("#{association}[#{id}]", new_object, child_index: id) do |builder|
#byebug
render( partial, f: builder)
end
fields = CGI.escapeHTML(fields).html_safe
link_to(name, '#', class: html_options[:class], data: {id: id, fields: fields})
end
end
add to your app/assets/application.js
$(document).on('click', '.custom_remove_fields', function(event) {
$(this).prev('input[type=hidden]').val('1');
$(this).closest('tr').hide();
return event.preventDefault();
});
$(document).on('click', '.custom_add_fields', function(event) {
var regexp, time;
time = new Date().getTime();
regexp = new RegExp($(this).data('id'), 'g');
$('.contact_fields').append($(this).data('fields').replace(regexp, time));
return event.preventDefault();
});
In your template you can use the following to render a partial for the collection:
<%= link_to_add_row('contacts', contact.new, 'contact_fields', class: 'btn btn-primary') do %>
<i class="fas fa-plus"></i>
<% end %>
This is how I render the partial with the collections in my template:
<tbody class="contact_fields">
<% #contacts.each_with_index do |contact, index| %>
<%= fields_for "contacts[#{index}]", contact do |c| %>
<%= render 'contact_fields', f: c %>
<% end %>
<% end %>
</tbody>
This is what my contact_fields.html.erb partial looks like.
<tr class="nested-fields">
<td>
<%= f.text_field :fullname, class: 'form-control invoke-contacts-search contact-fullname' %>
</td>
<td>
<%= f.text_field :email, class: 'form-control invoke-contacts-search contact-email' %>
</td>
<td>
<%= f.text_field :phone, class: 'form-control contact-phone' %>
</td>
<td>
<%= f.text_field :department, class: 'form-control contact-department' %>
</td>
<td>
<%= f.text_field :manager, class: 'form-control contact-manager' %>
</td>
<td>
<%= f.hidden_field :id %>
<%= f.hidden_field :_destroy %>
<%= link_to '#', class: 'btn btn-danger custom_remove_fields' do %>
<i class="fas fa-trash-alt"></i>
<% end %>
</td>
</tr>

How to DRY two slightly different forms?

I know about partial in rails, but it seems that I can only re-use exactly the same form with partial.
What if I have two forms that differ only in one field? For example, in my classroom app, I have one form for a peer-grader and one form for a teacher-grader.
The teacher's form is as follows. The peer-grader form is exactly the same, minus the grade field. How do I DRY this?
<%= form_for #doc,
url: student_homework_document_path(student_id: #doc.submitter_id,
id: #doc.id),
html: { multipart: true } do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.label :grade %>
<%= f.number_field :grade %>
<%= f.label :graded_file %>
<%= f.file_field :graded_file %>
<%= f.label :graded_file_source_code %>
<%= f.file_field :graded_file_source_code %>
<%= f.submit "Submit grading", class: "btn btn-large" %>
<% end %>
I would have thought you had one controller called teachers and one called peers or something. You could keep the form the same but add a variable to the peer-grader method such as #peer_grader = true. Then in your form add:
<% if #peer_grader = true %>
<%= f.label :grade %>
<%= f.number_field :grade %>
<% end %>
You would also need to set another variable called #url in both controllers set to the correct path and then update the form to:
<%= form_for #doc, url: #url, html: { multipart: true } do |f| %>

I have created a netsted form and action, but keep getting a to_key error

I'm trying to follow along with a tutorial. I am using the same methods as a Blog Post and Blog Comment type tutorial, just trying something a little different. I keep getting an error:
undefined method `to_key' for #(had to remove the first <)Agentinteraction::ActiveRecord_Relation:0x007fa4d43ca138>
With the errors highlighted as: NoMethodError in Agents#show
<% form_for #qrreviews, :url => create_qrreview_agents_path do |f| %>
Routes:
resources :agents do
collection do
post 'create_qrreview', to: 'agents#create_qrreview'
end
end
Agents Show Action:
def show
#qrreviews = Agentinteraction.where("agent_id = ?", #agent.id)
#agentinteraction = Agentinteraction.new
end
show.html.erb
<p id="notice"><%= notice %></p>
<%= #agent.name %>
<%= #agent.email %>
<%= #agent.team %>
<%= link_to 'Edit', edit_agent_path(#agent) %> |
<%= link_to 'Back', agents_path %>
QR Feedback:<br/><br/>
<%#= #comments.blog %>
<% #qrreviews.each do |qrreview| %>
<%= qrreview.agent_id %><br>
<%= qrreview.yes_or_no %><br>
<% end %>
<br/><br/>
<% form_for #qrreviews, :url => create_qrreview_agents_path do |f| %>
Category:
Interaction:
Act One Score:
Act Two Score:
Act Three Score:
Type of Interaction:
Training Needed?
Type of Training Needed?
<%= f.submit %>
<% end %>
undefined method `to_key' for Agentinteraction::ActiveRecord_Relation:0x007fa4d43ca138
The problem is you have define #qrreviews = Agentinteraction.where("agent_id = ?", #agent.id) which returns a AR_Relation.
Try changing
#qrreviews = Agentinteraction.where("agent_id = ?", #agent.id)
to
#qrreviews = Agentinteraction.where("agent_id = ?", #agent.id).first
Also
<% form_for #qrreviews, :url => create_qrreview_agents_path do |f| %>
should be
<%= form_for #qrreviews, :url => create_qrreview_agents_path do |f| %>

What is wrong with my coffeescript in Stripe?

I've been working on integrating Stripe into my web application, and it doesn't seem to be working. To help me along, I've been using Ryan Bates's Rails Cast on integrating Stripe. Whenever I try to run the payment form, I get an error saying that "There was a problem with my credit card". I think the problem lies in my coffeescript file, but perhaps I'm wrong. I've included the stripe user token as a part of my user model instead of placing it into its own subscription model. Here is the coffeescript code I have:
jQuery ->
Stripe.setPublishableKey($('meta[name="stripe-key"]').attr('content'))
subscription.setupForm()
user =
setupForm: ->
$('#new_user').submit ->
$('input[type=submit]').attr('disabled', true)
if $('#card_number').length
user.processCard()
false
else
true
processCard: ->
card =
number: $('#card_number').val()
cvc: $('#card_code').val()
expMonth: $('#card_month').val()
expYear: $('#card_year').val()
Stripe.createToken(card, user.handleStripeResponse)
handleStripeResponse: (status, response) ->
if status == 500
$('#user_stripe_card_token').val(response.id)
$('#new_user')[0].submit()
else
$('#stripe_error').text(response.error.message)
$('input[type=submit]').attr('disabled', false)
I'm a beginner when it comes to programming, so any help you can give me would be great.
Here's the error I get in my terminal when I try to sign up:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Xas+iA+a3op7jUi57qTr7XWQSClPscA7fR19rkclkEE=", "user"=>{"stripe_card_token"=>"", "name"=>"Jack", "email"=>"email#example.com", "phone_number"=>"203-xxx-xxxx", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Create my account"}
User Exists (0.2ms) SELECT 1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER('jjets718#yahoo.com') LIMIT 1
Stripe error while creating customer: Invalid token id:
My view for the signup is this:
<% provide(:title, 'Sign up') %>
<h1>Sign up</h1>
<div class="row">
<div class="span6 offset3">
<%= form_for(#user) do |f| %>
<%= render 'shared/error_messages' %>
<%= f.hidden_field :stripe_card_token %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :email %>
<%= f.text_field :email %>
<%= f.label :phone_number, "Your cell phone number" %>
<%= f.text_field :phone_number %>
<%= f.label :password %>
<%= f.password_field :password %>
<%= f.label :password_confirmation, "Password confirmation" %>
<%= f.password_field :password_confirmation %>
<%= label_tag :card_number, "Credit Card Number" %>
<%= text_field_tag :card_number, nil, name: nil %>
<%= label_tag :card_code, "Security Code on Card (CVV)" %>
<%= text_field_tag :card_code, nil, name: nil %>
<%= label_tag :card_month, "Card Expiration" %>
<%= select_month nil, {add_month_numbers: true}, {name: nil, id: "card_month"}%>
<%= select_year nil, {start_year: Date.today.year, end_year: Date.today.year+15}, {name: nil, id: "card_year"} %>
<%= f.submit "Create my account", class: "btn btn-large btn-primary" %>
<% end %>
</div>
</div>
<div id="stripe_error">
<noscript>JavaScript is not enabled and is required for this form. First enable it in your web browser settings.</noscript>
</div>
My code for my controller is this for the create method:
def create
#user = User.new(params[:user])
if #user.save_with_payment
sign_in #user
flash[:success] = "Welcome to the Sample App!"
redirect_to edit_user_path(current_user)
UserMailer.welcome_email(#user).deliver
else
render 'new'
end
end
My code for the database migration for the user token is this:
class AddStripeToUsers < ActiveRecord::Migration
def change
add_column :users, :stripe_customer_token, :string
end
end
And the code for the save_with_payment method in my model is this:
def save_with_payment
if valid?
customer = Stripe::Customer.create(description: email, plan: 1, card: stripe_card_token)
self.stripe_customer_token = customer.id
save!
end
rescue Stripe::InvalidRequestError => e
logger.error "Stripe error while creating customer: #{e.message}"
errors.add :base, "There was a problem with your credit card."
false
end
2 things that come to mind:
You should be doing a status check for 200, not 500
You may need to require the coffeescript file in your application.js
e.g. //= require users
I could be wrong, but at this point:
handleStripeResponse: (status, response) ->
if status == 500
$('#user_stripe_card_token').val(response.id)
In addition to changing if status == 500 to if status == 200, this line $('#user_stripe_card_token').val(response.id) may need to be $('#new_user_stripe_card_token').val(response.id). Make sure to check the input ID.

Rails 3 form_for helper not submitting action correctly

I have this form:
<% #page_title = "Delete Technician: #{#technician.name}" %>
<%= link_to("<< Back to List", {:action => 'list', :id => #technician.id}, :class => 'back-link') %>
<div class="technician delete">
<h2>Delete Technician</h2>
<%= form_for(:technician, :url => {:action => 'destroy', :id => #technician.id}) do |f| %>
<p>Are you sure you want to permanently delete this technician?</p>
<p class="reference-name"><%= #technician.name %></p>
<div class="form-buttons">
<%= submit_tag("Delete Technician") %>
</div>
<% end %>
</div>
when I click on the submit button this is the url that I get:
www.site.com/technicians/1
instead of
www.site.com/technicians/destroy/1
am I not using the form_for helper correctly or is it a configuration somewhere?
You're making this more complicated than it needs to be. There is no reason for a form when a link or a button would do. Why not just this
<p>Are you sure you want to permanently delete this technician</p>
<div> <%= link_to "Delete", technician_path(#technician), :method => :delete %> </div>
That's it.