How do I render stripe elements form twice on one page - ruby-on-rails

Hopefully someone can help me out with this! I am trying to implement stripe elements in rails and basically what I have is an Orders class, but the person checking out has the option to choose between two forms... the first form has two additional field options for them to fill out if the click this option. The second option has two less fields to fill out in the order form. Right now I am rendering this form as a partial and I am restricting whether or not the two conditional fields are shown by passing a local to the form partial. This is all working great. What is NOT working, is that my Stripe elements tag is rendering fine on the first form render, but it never renders in a functional state or with any css styles on the second form. The stripe_elements_tag in the second rendered version of the tag is totally useless. Can anyone think of a better way to do this or a fix that might work? I tried hiding the content in the divs and displaying it with an on click, show the form, but this also did not work. The second form would never be shown ( i noticed this option also made the form animations slower :( .
At this point I am considering actually just writing two separate forms even though thats not very DRY just to see if it will work that way.
Any ideas or thoughts are very much welcomed!
Have a great day everyone!
here is my new.html.erb code
<%= stripe_javascript_tag %>
<div class="container">
<div class="mx-auto" width="400px">
<div class="accordion" id="accordionExample">
<div class="card">
<div class="card-header" id="headingOne">
<h5 class="mb-0">
<button class="btn btn-link" type="button" data-toggle="collapse" data-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
Referred By Group
</button>
</h5>
</div>
<div id="collapseOne" class="collapse" aria-labelledby="headingOne" data-parent="#accordionExample">
<div class="card-body">
<%= render "order_form", locals: { buy_method: "group" } %>
</div>
</div>
</div>
<div class="card">
<div class="card-header" id="headingTwo">
<h5 class="mb-0">
<button class="btn btn-link collapsed" type="button" data-toggle="collapse" data-target="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
Purchasing Independently
</button>
</h5>
</div>
<div id="collapseTwo" class="collapse" aria-labelledby="headingTwo" data-parent="#accordionExample">
<div class="card-body">
<%= render "order_form", locals: { buy_method: "individual" } %>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="col-md-6" id="flash-message">
<%= render partial: 'flash' %>
<% #order.errors.full_messages.each do |msg| %>
<li> * <%= msg %> </li>
<% puts msg %>
<% end %>
</div>
<div class="row">
<div class="col-md-9 mb-md-0 mb-5">
<%= form_for #order do |f| %>
<div class="row">
<div class="col-md-6">
<div class="md-form mb-0">
<%= f.label :customer_name, "Your Name *" %><br />
<%= f.text_field :customer_name, class: "form-control" %>
</div>
</div>
<div class="col-md-6">
<div class="md-form mb-0">
<%= f.label :email, "Email *" %><br />
<%= f.text_field :email, class: "form-control" %>
</div>
</div>
</div>
<% if locals[:buy_method] == "group"%>
<div class="row">
<div class="col-md-6">
<div class="md-form mb-0">
<%= f.label :student_name, "Student Name *" %><br />
<%= f.text_field :student_name, class: "form-control" %>
</div>
</div>
<div class="col-md-6">
<div class="md-form mb-0">
<%= f.label :school_name, "School or Group Name *" %><br />
<%= f.text_field :school_name, class: "form-control"%>
</div>
</div>
</div>
<% end %>
<div class="row">
<div class="col-md-6">
<div class="md-form mb-0">
<%= f.label :street_address, "Street Address *" %><br />
<%= f.text_field :street_address, class: "form-control" %>
</div>
</div>
<div class="col-md-6">
<div class="md-form mb-0">
<%= f.label :number_books, "Number of books to purchase *" %><br />
<%= f.select :number_books, (0..99), class: "form-control", selected: 0%>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="md-form mb-0">
<%= f.label :city, "City *" %><br />
<%= f.text_field :city, class: "form-control" %>
</div>
</div>
<div class="col-md-6">
<div class="md-form mb-0">
<%= f.label :state, "State *" %><br />
<%= f.select :state, [ "--",'AL', 'AK', 'AS', 'AZ', 'AR', 'CA', 'CO', 'CT', 'DE', 'DC', 'FM', 'FL', 'GA', 'GU', 'HI', 'ID', 'IL', 'IN', 'IA', 'KS', 'KY', 'LA', 'ME', 'MH', 'MD', 'MA', 'MI', 'MN', 'MS', 'MO', 'MT', 'NE', 'NV', 'NH', 'NJ', 'NM', 'NY', 'NC', 'ND', 'MP', 'OH', 'OK', 'OR', 'PW', 'PA', 'PR', 'RI', 'SC', 'SD', 'TN', 'TX', 'UT', 'VT', 'VI', 'VA', 'WA', 'WV', 'WI', 'WY' ], class: "form-control"%>
</div>
</div>
<div class="col-md-6">
<div class="md-form mb-0">
<%= f.label :zip_code, "Zip code *" %><br />
<%= f.text_field :zip_code, class: "form-control" %>
</div>
</div>
</div>
<br>
<div class="row">
<div class="col-md-6">
<div class="checkbox">
<%= f.check_box :email_permission, checked: "checked", checked_value: true, unchecked_value: false %>
<%= f.label :email_permission, class: "checkbox-inline" %>
</div>
</div>
</div>
<div class="form-group">
<label for="card-element">Credit or debit card *</label>
<div id="card-element" class="form-control" >
<%= stripe_elements_tag submit_path: contact_index_path %>
</div>
</div>
<div class="form-group">
<%= f.label :total %>
<%= f.label :total, id: "total", value: "$#{ #order.total }" %>
</div>
<%= f.submit "Submit", class: "btn btn-default btn-primary" %>
</div>
<% end %>
</div>
</div>

You need to create two separate Instances of the card referencing your stripe api_key two separate times. You cannot create two elements using the same instantiation of your stripe api key.

Related

How can I rearrange my params to get the desired structure?

I am on a rails 5.07 app. I have some params coming in from a form and that forms partial. I need to restructure these params or re-wire my model associations or try to implement Nested Forms...WHATEVER incantation I need to do in order to restructure / reorder the params from...
Note: I'm not sure how I can be more specific or ask to do only one thing here. As I am asking to do "only one thing"....sooo before you close my question....please be specific as to why? and give an example of how I can ask...this one thing... to rearrange params, differently?
current params
> {"utf8"=>"✓",
> "assignment"=>{"volunteer_shift_attributes"=>{"volunteer_task_type_id"=>"41",
> "roster_id"=>"7", "program_id"=>"9", "set_description"=>"Nov10",
> "set_date"=>"2021-01-08"}, "start_time(1i)"=>"2021",
> "start_time(2i)"=>"11", "start_time(3i)"=>"10",
> "start_time(4i)"=>"08", "start_time(5i)"=>"00",
> "end_time(1i)"=>"2021", "end_time(2i)"=>"11", "end_time(3i)"=>"10",
> "end_time(4i)"=>"09", "end_time(5i)"=>"00", "notes"=>"Nov10",
> "contact_id"=>"166574", "closed"=>"0", "lock_version"=>"0"},
> "contact_element_prefix"=>"contact", "commit"=>"Submit",
> "controller"=>"volunteer_events", "action"=>"create_shift"}
to Desired params structure
{"assignment"=>{"volunteer_shift_attributes"=>
{"volunteer_task_type_id"=>"41", "roster_id"=>"7", "program_id"=>"9",
"set_description"=>"fonso says wtf?"}, "set_date"=>"2021-01-07",
"contact_id"=>"166574", "closed"=>"0", "start_time(4i)"=>"12",
"start_time(5i)"=>"00", "end_time(4i)"=>"13", "end_time(5i)"=>"00",
"notes"=>"fuuuuuuuuuk"}, "lock_versions"=>["0"],
"contact_element_prefix"=>"contact", "commit"=>"Update",
"controller"=>"volunteer_events", "action"=>"create_shift"}
so I can say something like...
params["assignment"]["volunteer_shift_attributes"]["roster_id"]
and result being "7"
Please let me know if I need to include my model associations or anything else. Happy to oblige.
UPDATE:
per request, adding the form and the partial below...
here is the main form that produces the undesired outcome
<!--This is NEW action form-->
<%= form_for #assignment, :url => #my_url, remote: true do |f| %>
<!-- #FIXME need a fields_for 4 the volunteer_event-->
<div class="">
<div class="modal-body d-flex">
<div class="col-sm-8 border-right">
<section id="location-date-time-notes" class="flex">
<% if #assignment.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#assignment.errors.count, "error") %> prohibited this assignment from being saved:</h2>
<ul>
<% #assignment.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<!--VOLUNTEER SHIFT-->
<!--TODO: make this a partial under field_for-->
<%= f.fields_for :volunteer_shift do |builder| %>
<%= render 'assignments/volunteer_shift_fields', vs: builder %>
<% end %>
<!--TODO: Volunteer Shift end -->
<div id="time-row" class="d-flex flex-row">
<label for="assignment_time" class="col-sm-3 p-2">
<i class="fa fa-clock-o fa-lg" aria-hidden="true"></i> Time:
</label>
<div class="col- p-2">
<div class="myStartTime" id="start_time_<%= #assignment.id %>">
<%= f.time_select :start_time %>
</div>
</div>
<div class="col- p-2"><i class="fa fa-arrows-h fa-lg" aria-hidden="true"></i></div>
<div class="col-sm-3 p-2">
<div class="myEndTime" id="end_time_<%= #assignment.id %>">
<%= f.time_select :end_time %>
</div>
</div>
</div>
<div class="d-flex flex-row">
<label for="assignment_notes" class="col-sm-3 p-2">
<i class="fa fa-clipboard fa-lg" aria-hidden="true"></i> Notes:
</label>
<div class="col-sm-9 p-2">
<div class="d-flex flex-row">
<span> Notes only get saved if a contact is assigned the shift, and get removed when the contact is removed from the shift.</span>
<div class="">
<%= f.label :notes %>
<%= f.text_area :notes %>
</div>
</div>
</div>
</div>
</section>
</div>
<div class="col-sm-4">
<!-- Contact Section-->
<div id="contact_section">
<% if #assigned_contacts && #assigned_contacts.length > 0 %>
<h2>Previously Assigned Contacts</h2>
<% #assigned_contacts.each do |c| %>
<%= label_tag "assigned_contacts[#{c.id}]", "Are you sure you want to remove the currently scheduled volunteer, #{c.display_name} (##{c.id}), from the assignment(s)?" %>
<%= check_box_tag "assigned_contacts[#{c.id}]", "replace", #replaced_contacts.include?(c.id) %>
<% end %>
<% end %>
<input id="contact_element_prefix" name="contact_element_prefix" type="hidden" value="contact">
<div class="name large flex-row">
<%= f.label :contact_id %><%= f.text_field :contact_id %>
</div>
<div id="display-contact" class="d-flex flex-row">
<% if f.object.contact_id %>
<%= render partial: 'contacts/contact_display', locals: { contact:f.object.contact} %>
<% else %>
<div>no contact attatched- _form.html called</div>
<%#= link_to 'Show Contact', contact_path(f.object.contact_id), remote: true %>
<% end %>
</div>
<!-- FIXME: replace this logic stack with AJAX-->
<%#= contact_field("#obj", "contact_id",
:locals => {:options => {
:object_name => f.object_name.to_s,
:field_name => 'contact_id',
:on_display => 'display_disciplinary_notes(); display_contact_notes();'
}}
) %>
<%= f.label :closed, "Is this slot closed?" %>
<%= f.check_box :closed %>
<!--Contact Section END-->
<!--Attendance / Call Status start-->
<% if f.object.id && f.object.contact_id %>
<div class="flex-row">
<div class="col-25"><label for="assignment_attendance_type_id">Attendance:</label></div>
<div class="col-75"><%= select(f.object_name,
"attendance_type_id",
AttendanceType.all.sort_by(&:id).collect {|p| [ p.name, p.id ] },
:include_blank => true) %></div>
</div>
<div class="flex-row">
<div class="col-25"><label for="assignment_call_status_type_id">Call status:</label></div>
<div class="col-75"><%= select(f.object_name,
"call_status_type_id",
([["not called yet", ""]] + CallStatusType.all.sort_by(&:id).collect {|p| [ p.name, p.id ] }),
:include_blank => false) %></div>
</div>
<% end %>
<!-- Attendance / Call Status End-->
<!-- LOCK VERSION-->
<div class="flex-row">
<div class="col-25">
<%= f.label :lock_version %>
</div>
<div class="col-75">
<%= f.number_field :lock_version %>
</div>
</div>
<!-- LOCK end-->
</div>
</div>
</div>
<div class="modal-footer d-flex justify-content-between">
<div class="edit_icons d-flex flex-row">
<div class="d-flex flex-column">
<!-- <i class="fa fa-share-alt-square fa-lg" aria-hidden="true"></i> Split-->
<!-- <i class="fa fa-files-o fa-lg" aria-hidden="true"></i> Copy-->
</div>
<div class="d-flex flex-column">
<%#= link_to '<i class="fa fa-pencil-square-o fa-lg" aria-hidden="true"></i>Edit'.html_safe, edit_assignment_path, remote: true%>
<!-- <i class="fa fa-refresh fa-lg" aria-hidden="true"></i> Reassign-->
</div>
</div>
<div>
<button type="button" class="btn btn-secondary mr-2" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary"><%= f.submit "Submit" %></button>
<!-- <input id="assignment_submit" name="commit" type="submit" value="Update">-->
</div>
</div>
</div>
<% end %>
and the partial file _volunteer_shift_fields
<!--<div class="name large flex-row">-->
<%= vs.label :volunteer_shift %>
<!--</div>-->
<div id="volunteer_shift" class="d-flex flex-row">
<div class="col-sm-12 p-2">
<div id="volunteer_shift" class="text-right">
<div class="field">
<%= vs.label :volunteer_task_type_id %>
<%= vs.select 'volunteer_task_type_id', options_from_collection_for_select([VolunteerTaskType.new(:description => ""), VolunteerTaskType.instantiables.effective_on(Date.today)].flatten, "id", "description") %>
</div>
<div class="field">
<%= vs.label :roster_id %>
<%= vs.select 'roster_id', options_from_collection_for_select([Roster.new(:name => ""), Roster.all].flatten, "id", "name") %>
</div>
<div class="field">
<%= vs.label :program_id %>
<%= vs.select 'program_id', options_from_collection_for_select([Program.new(:name => ""), Program.where(:volunteer => true)].flatten, "id", "name")%>
</div>
<div class="field">
<%= vs.label :set_description, "Description" %>
<%= vs.text_area(:set_description) %>
</div>
<div class="field">
<%= vs.label :set_date, "Date" %>
<%= vs.text_field(:set_date) %>
</div>
</div>
</div>
</div>
UPDATE 2:
Big thanks to sam. adjusting my partial has gotten me 90% there. I "think" I just need to move the remaining form elements from parent to child and vice versa to get the rest of the way there. I'll post the outcome once I've tried that as well.
in your _volunteer_shift_fields file, try to bind your form element with the form object. Replace select_tag with vs.select, text_field with vs.text_field. Then Rails will automatically build the desired params.

Dynamically change radio button with select (Ruby on Rails)

I'm new at programming and Rails in particular.
I have a form to create new goal records.
I have a select to chose the goal "Horizon": "weekly", "quarterly", or "yearly"
I also have a radio button, "Due date" to set the goal as for this term or next term (e.g. in the case of a weekly goal: this week or next week).
What I would like to do: is to dynamically update the radio button, based on the selected option of the select.
<%= form_with(model: #goal, url: goals_path, id: "new-goal-form", local: true) do |f| %>
<p><%= #goal.errors[:date].first %></p>
<div class="field">
<label class="label">Title</label>
<div class="control">
<%= f.text_field :title, class: "input"%>
</div>
<p><%= #goal.errors[:title].first %></p>
</div>
<div class="field">
<label class="label">Description</label>
<div class="control">
<%= f.text_area :description, class: "textarea"%>
</div>
</div>
<div class="field">
<label class="label">Horizon</label>
<div class="control">
<div class="select">
<%= f.select :horizon, options_for_select({"Weekly" => "week", "Quarterly" => "quarter", "Yearly" => "year"}, #horizon), include_blank: true %>
</div>
</div>
</div>
<div class="field">
<label class="label">Due Date</label>
<div class="control">
<%= f.radio_button :date, DateTime.current.to_date.end_of_week.strftime('%Y-%m-%d'), :checked => true, id:"radio-this", class: "is-checkradio" %>
<label for="radio-this">
This <%= #horizon.capitalize %>
</label>
<%= f.radio_button :date, 1.week.from_now.to_date.end_of_week.strftime('%Y-%m-%d'), id:"radio-next", class: "is-checkradio" %>
<label for="radio-next">
Next <%= #horizon.capitalize %>
</label>
</div>
</div>
<div class="field">
<label class="label">Related goal</label>
<div class="control">
<div class="select">
<%= f.select :related_goal_id, options_from_collection_for_select(#related_goal, 'id', 'title'), include_blank: true %>
</div>
</div>
</div>
<br/>
<div class="field is-grouped">
<div class="control">
<%= f.submit "Save", class: "button is-primary" %>
</div>
<div class="control">
<%= link_to "Cancel", goals_path(horizon: #horizon), class: "button" %>
</div>
</div>
<% end %>
I have tried adding :onchange at the end of the select, but nothing actually happens when changing the selected option.
Many thanks in advance.
Are your radio buttons id'd differently? They seem to both have the same autogenerated id. That won't work in javascript. Down below you set a click event for the select box and depending on what child is selected, you set a radio button checked to true.
$(document).ready(function(){
//when select option is chosen, set radio button to selected.
$("#select").click(function(){
var option = $(this).children("option:selected").val()
if(option == "weekly"){
$("#radio_button_weekly").prop("checked", true);
}
)}
)};

ruby on rails record not being updated

I'm using devise on my project, i want to update others profile from my admin panel, so only users with admin role can access it, since this behavior is very different to the default devise, i decided to create a separated controller, so i can manipulate users as normal records, but for some reason when i update users, the records are not updated into the database.
THESE ARE MY ROUTES
devise_for :users, controllers: { registrations: 'users/registrations' }
root "users#index"
post "users/:id" => "users#show"
get "users/:id" => "users#show", as: :user
patch "users/:id" => "users#show"
resources :receipts
resources :notes
get "users/edit/:id" => "users#edit", as: :edit_user
THIS IS MY CONTROLLER
def edit
#user = User.find(params[:id])
end
def update
user = User.find(params[:id])
user.update(user_update_params)
redirect_to user
end
private
def user_update_params
params.require(:user).permit(:username, :names, :last_names, :guardian, :phone, :identification, :role)
end
def user_params
if #user.debts.nil?
params.require(:user).permit(:debts)
else
params.require(:user).permit(:add_debt)
end
end
THESE IS MY VIEW
<% if current_user && current_user.has_role?(:admin) %>
<div class="container form-bg">
<div class="row main">
<div class="panel-heading">
<div class="panel-title text-center">
<h1 class="title">Editar usuario</h1>
<hr />
</div>
</div>
<div class="main-login main-center">
<%= form_for #user do |f| %>
<div class="form-group">
<%= f.label :username, "Usuario" ,class: "cols-sm-2 control-label" %><br />
<div class="cols-sm-10">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-user fa" aria-hidden="true"></i></span>
<%= f.text_field :username, autofocus: true, class: "form-control" %>
</div>
</div>
</div>
<div class="form-group">
<%= f.label :names, "Nombres" ,class: "cols-sm-2 control-label" %><br />
<div class="cols-sm-10">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-user fa" aria-hidden="true"></i></span>
<%= f.text_field :names, autofocus: true, class: "form-control" %>
</div>
</div>
</div>
<div class="form-group">
<%= f.label :last_names, "Apellidos" ,class: "cols-sm-2 control-label" %><br />
<div class="cols-sm-10">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-user fa" aria-hidden="true"></i></span>
<%= f.text_field :last_names, autofocus: true, class: "form-control" %>
</div>
</div>
</div>
<div class="form-group">
<%= f.label :phone, "Teléfono" ,class: "cols-sm-2 control-label" %><br />
<div class="cols-sm-10">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-phone fa" aria-hidden="true"></i></span>
<%= f.text_field :phone, autofocus: true, class: "form-control" %>
</div>
</div>
</div>
<div class="form-group">
<%= f.label :identification, "Número de identification" ,class: "cols-sm-2 control-label" %><br />
<div class="cols-sm-10">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-envelope fa" aria-hidden="true"></i></span>
<%= f.text_field :identification, autofocus: true, class: "form-control" %>
</div>
</div>
</div>
<div class="form-group">
<%= f.label :guardian, "Guardian legal" ,class: "cols-sm-2 control-label" %><br />
<div class="cols-sm-10">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-user fa" aria-hidden="true"></i></span>
<%= f.text_field :guardian, autofocus: true, class: "form-control" %>
</div>
</div>
</div>
<div class="form-group ">
<label>
<%= f.radio_button :role, "student" , :checked => true %>
Estudiante
</label>
<label>
<%= f.radio_button :role, "teacher" %>
Profesor
</label>
</div>
<div class="form-group">
<%= f.label :password, "Contraseña" ,class: "cols-sm-2 control-label" %><br />
<div class="cols-sm-10">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-lock fa" aria-hidden="true"></i></span>
<% if #minimum_password_length %>
<em>(<%= #minimum_password_length %> characters minimum)</em>
<% end %>
<%= f.password_field :password, autofocus: true, class: "form-control", autocomplete: "off" %>
</div>
</div>
</div>
<div class="form-group">
<%= f.label :password_confirmation, "Confirmar contraseña" ,class: "cols-sm-2 control-label" %><br />
<div class="cols-sm-10">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-lock fa" aria-hidden="true"></i></span>
<%= f.password_field :password_confirmation, autofocus: true, class: "form-control", autocomplete: "off" %>
</div>
</div>
</div>
<div class="form-group ">
<%= f.submit "Editar", class: "btn btn-primary btn-lg btn-block login-button" %>
</div>
<% end %>
</div>
</div>
<p class="invisible">asdsadsadsdsadsad</p>
</div>
<% else %>
<h1>Acceso exclusivo para administradores</h1>
<% end %>
So after reading my stack trace, it indeed tries to make the patch, but then it says that the params are not permitted, which is very weird since i already did permit them on my user_update_params method.
this what appears on my trace
Started PATCH "/users/12" for ::1 at 2017-01-28 13:20:53 -0500
Processing by UsersController#show as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"IdJKYYFVh8ZJJm/RPRJd7blfQw84Ct38KjE5tZ6fTuHFK8TG04MxUu2OtQv6wfLvFJll0EYYZa+ooxP19RqOAQ==", "user"=>{"username"=>"jimena", "names"=>"Jimena44", "last_names"=>"Delgado Díaz44", "phone"=>"214123244", "identification"=>"32312", "guardian"=>"3213123", "role"=>"student", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Editar", "id"=>"12"}
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 12]]
Unpermitted parameters: username, names, last_names, phone, identification, guardian, role, password, password_confirmation
So why are my files not being updated if it even redirects as it is intended on the controller?
Thanks for reading.
Since you are using devise I think you need to set it up like this:
before_action :configure_permitted_parameters
protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:account_update) << [:username, :names]
end
Make sure the controller is inheriting from devise:
class UsersController < Devise::RegistrationsController
You can read more about it here and here, in the parts about strong parameters.

How to wrap my Ruby on Rails partials in a Tabbed Boostrap Signup Form

I have used MTI (Multiple Table Inheritance) structures to create different SignUp Forms for my users such that I have 3 different partials called _company.html.erb, _individual.html.erb, and _admin.html.erb, since they have different details to be used for signing up into the app am working on. However, I want to render each partial such that I could achieve a TabbedForm based on Bootstrap.
For more clarity, I like to achieve something like this picture below:
Here is what I have done:
SignUp View
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3">
<div class="panel panel-login">
<div class="panel-heading">
<div class="row">
<div class="col-xs-6">
Login
</div>
<div class="col-xs-6">
Register
</div>
</div>
<hr>
</div>
<div class="panel-body">
<div class="row">
<div class="col-lg-12">
<%= render 'companies/form' %>
<%= render 'individuals/form' %>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
One of the Partials rendered in the Signup View above _form.html.erb
<%= simple_form_for(Company.new) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :company_name, label: "Company Name", placeholder: 'Company Name' %>
<%= f.input :company_url, label: "Company URL", placeholder: 'Company URL' %>
<%= f.input :country, label: "Country", placeholder: 'Country' %>
<%= f.input :contact_person, label: "Contact Person", placeholder: 'Contact Person' %>
<%= f.input :phone_number, label: "Phone Number", placeholder: 'Phone Number' %>
<%= f.input :email, required: true, autofocus: true %>
<%= f.input :password, required: true, hint: ("#{#minimum_password_length} characters minimum" if #minimum_password_length) %>
<%= f.input :password_confirmation, required: true %>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
How do I wrap my Partials in Bootstrap Tabs such that I can have a TabbedForm like the provided picture above?
try this on your sign up view file:
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3">
<div class="panel panel-login">
<div class="panel-heading">
<div class="row">
<div class="col-xs-6">
Login
</div>
<div class="col-xs-6">
Register
</div>
</div>
<hr>
</div>
<div class="panel-body">
<div class="row">
<!-- Nav tabs -->
<ul class="nav nav-tabs" role="tablist">
<li role="presentation" class="active">
Individual
</li>
<li role="presentation">
Company
</li>
</ul>
<!-- Tab panes -->
<div class="tab-content">
<div role="tabpanel" class="tab-pane active" id="individual">
<%= render 'individuals/form' %>
</div>
<div role="tabpanel" class="tab-pane" id="company">
<%= render 'companies/form' %>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<script type="text/javascript">
$('#myTabs a').click(function (e) {
e.preventDefault()
$(this).tab('show')
});
</script>
NOTE: you need to have bootstrap javascript included in your view file for this to work.

Wrong number of Arguments ( 3 for 2 ) in Rails in f.select

I have this multiple select list which gives me an error Wrong number of Arguments ( 3 for 2 ) in a form_form a "Topic" model which has_many :curriculums, :through => another_model
<%= f.select :curriculum_ids, options_from_collection_for_select(Curriculum.all, :id, :name, #topic.curriculum_ids), {}, { "data-placeholder"=>"Select Curriculum", :multiple=>true , :class=>"chzn-select"} %>
However this code works just fine in another controller's view :
Here we have a Question model with has_and_belongs_to_many :skills
<%=f.select :skill_ids, options_from_collection_for_select(Skill.all, :id, :name, #question.skill_ids),{},{"data-placeholder"=>"Select Other Skills",:multiple=>true,:class=>"chzn-select"}%>
Here is the complete _form.html.erb :
<div class="row-fluid attributeContainer">
<div class="span12">
<% if #topic.errors.any? %>
<% #topic.errors.full_messages.each do |msg| %>
<div class="alert alert-error">
<button class="close" data-dismiss="alert">×</button>
<strong>Error!</strong> <%= msg %>
</div>
<% end %>
<% end %>
<div class="widget-box">
<div class="widget-title">
<span class="icon">
<i class="icon-align-justify"></i>
</span>
<h5>Topics</h5>
</div>
<div class="widget-content nopadding">
<%= form_for(#topic,:html=>{:class=>"form-horizontal"}) do |f| %>
<div class="control-topic">
<label class="control-label">Topic Name</label>
<div class="controls">
<%= f.text_field :name %>
</div>
</div>
<div class="control-group">
<label class="control-label">Description</label>
<div class="controls">
<textarea name="topic[description]" value="<%=#topic.description%>"><%=#topic.description%></textarea>
</div>
</div>
<div class="control-group">
<label class="control-label">Subject</label>
<div class="controls">
<div class="span3">
<%= f.collection_select(:subject_id, Subject.all, :id, :name, {:include_blank => 'Please Select Subject'}) %>
</div>
</div>
</div>
<div class="control-group">
<label class="control-label">Curriculum</label>
<div class="controls">
<div class="span3">
<%= f.select :curriculum_ids, options_from_collection_for_select(Curriculum.all, :id, :name, #topic.curriculum_ids), {}, { "data-placeholder"=>"Select Curriculum", :multiple=>true , :class=>"chzn-select"} %>
</div>
</div>
</div>
<div class="control-group attributeTemplate2" style="display:none;">
<label class="control-label">Concept Tag</label>
<div class="controls" >
<div class="span3" >
<%= f.collection_select(:name, Concept.all,:id, :name,{:include_blank => 'Please Select'},{:name=>"topic[concept][][concept_id]"}) %>
</div>
<div class="span2">
<%= f.text_field :name, :style=> "margin-left:25px;", :class=>'text_field', :name=>'topic[concept][][concept_sequence]', :placeholder=>'concept_sequence', :size=>'30'%>
</div>
<div>
<a style="margin-left:25px;" class="btn btn-danger removeRow"><i class="icon-white icon-remove-sign"></i></a>
</div>
</div>
</div>
<div class="control-group ">
<label class="control-label">Concept Tag</label>
<div class="controls" >
<div class="span3" >
<%= f.collection_select(:name, Concept.all,:id, :name,{:include_blank => 'Please Select'},{:name=>"topic[concept][][concept_id]",:class=>"chzn-select"}) %>
</div>
<div class="span2">
<%= f.text_field :name, :style=> "margin-left:25px;", :class=>'text_field', :name=>'topic[concept][][concept_sequence]', :placeholder=>'concept_sequence', :size=>'30'%>
</div>
<div>
<a style="margin-left:25px;" class="btn btn-danger removeRow"><i class="icon-white icon-remove-sign"></i></a>
</div>
</div>
</div>
<div class="control-group addTopicConcepts">
<div class="controls" >
<div><a class="btn btn-primary">Add Concept</a></div>
</div>
</div>
<div class="form-actions">
<button type="submit" class="btn btn-primary">Save</button>
</div>
<%end%>
</div>
</div>
</div>
</div>
CODE FIXED :
Apparently the join model had code :
belongs_to :curriculum , :grade, :topic
Changing to the code fixed everything.
belongs_to :curriculum
belongs_to :grade
belongs_to :topic
Why not use collection_select?
http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-collection_select
When used in form_for parameters are as follows
first parameter is the name of the field you wish to record the value to (e.g. :curriculum_id)
second parameter is the model you wish to collect items from (e.g. Curriculum.all)
third parameter is the field you want to record the value from (e.g. :id)
fourth parameter is the field you wish to display as the value (e.g. :name)
so for your example
<%= f.collection_select :curriculum_id, Curriculum.all, :id, :name, { "data-placeholder"=>"Select Curriculum", :multiple=>true , :class=>"chzn-select"} %>
should approximate what you want, though you may need to tweak it.

Resources