In my application, lists has_many books and books belong_to a list. I have this form for creating a new book in my rails application:
<div class="formtastic-control">
<%= semantic_form_for(#book) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.input :list, :as => :select, :include_blank => false %>
<%= f.text_field :title, class: 'form-control', placeholder: "Title" %>
<%= f.text_field :author, class: 'form-control', placeholder: "Author (optional)" %>
<%= f.submit "Add to library", class: "btn btn-default" %>
<% end %>
</div>
So the user can select which list they want to add the book to. I'm using formtastic to do that. The whole thing works fine, but I want to get rid of the word "list" which appears next to the select tag:
How can I do that? I know it's trivial, but it pushes the select tag to the right and I don't want it to do that.
I added :label => false and it worked, thanks.
Related
I'm trying to add Mapbox autofill to my address bar on rails, for mapbox autofill to work I need to have autocomplete="street-address in an input field.
I'm using simple_form_form and I can't figure out how to add autocomplete attribute. Here is my form:
<%= simple_form_for #lock do |f| %>
<div class="form_wrapper">
<div class="form_div">
<%= f.input :name %>
<%= f.input :description %>
<mapbox-address-autofill>
<%= f.input :address, :autocomplete => "street-address" %>
</mapbox-address-autofill>
<%= f.input :photo, as: :file, label: "Link to picture" %>
<%= f.input :special_content %>
<%= f.button :submit, :class => "btn btn-primary" %>
<% end %>
any ideas on how to make the autocomplete attribute work?
Use input_html and pass the additional options like class etc.
<%= f.input :address, input_html: { :autocomplete => "street-address"} %>
I am attempting to create a feature where users can add an existing record, recipe, to a collection of records, menu. I am using collection_select with a simple_form to allow users to select multiple records from a list however, the form is not responding to the input_html: { multiple: true } option, which should allow users to select multiple values. The form is as below, please let me know if any other code would be helpful for context.
Form:
<%= simple_form_for #menu, local: true do |f| %>
<%= f.label :title, :class => "form-component-header" %>
<%= f.text_field :title, :class => "form-field" %>
<%= f.label :recipe_ids %>
<%= f.collection_select :recipe_ids, f.object.user.recipes, :id, :title, input_html: { multiple: true } %>
<%= f.submit :class => "form_button" %>
<% end %>
.permit(....recipe_ids: [])
You need to update the permitted parameters in your controller. Now that you are sending multiple selections the parameter needs to be marked as expecting an array.
I have a drop down selector in a form and even after saving an object, when I go to my edit page, the drop down reverts to the first item. If submit is clicked, the value changes to the first item in the list.
In this case the drop down contains a list of states. Every time I go to the edit page, Alabama is selected and if I don't manually change the value back to what it initially was, the state becomes Alabama.
<%= simple_form_for #event, url: coin_event_path(#coin.id) do |f| %>
<%= f.input :content, :label => "Event Description", class: 'form-control' %>
<%= f.input :link, :label => "Link to Event", class: 'form-control' %>
<%= f.input :date, order: [:month, :day, :year], class: 'form-control' %>
<%= f.input :time, as: :time, html5: true, class: 'form-control' %>
<%= f.input :city, class: 'form-control' %>
<%= f.select :state, options_for_select(us_states),{}, class: 'form-control' %>
<%= f.input :description, :label => "Event Description", class: 'form-control' %>
<% if can? :destroy, Event %>
<%= f.select :accepted, [['Accepted', true], ['Not Accepted', false]] %>
<% end %>
<%= f.button :submit, 'Submit' %>
<%= link_to "Back", coin_path(#coin.id), class: "btn btn-default" %>
How do I change this so it stays on the state that it's supposed to?
You can using selected, like:
options_for_select(us_states, selected: "set_current_value")
More usage examples - options_for_select() docs.
I know the title sounded a bit wonky, but this is what I am trying to do.
I have two models - Job and Company. Someone can create a Job listing, but before the listing is saved, it should be associated with a company. This company can be newly created, or should be populated from companies the current_user has previously created.
A job belongs_to :company, and a company has_many :jobs.
I know I could do two different views, and just send the user to two different actions on two different controllers, but I would like to simplify it and just have everything done in one view.
When they go to /jobs/new, they should see the normal jobs/_form.html.erb partial, but I would love to be able to show either a Company dropdown for existing companies or new company creation fields that the user fills out and that new company gets associated with this new job that is being created.
This is what my jobs/_form.html.erb partial looks like:
<%= simple_form_for(#job) do |f| %>
<%= f.input_field :title %>
<%= f.input_field :salary %>
<%= f.input_field :description, as: :text %>
<%= f.input_field :apply_description, as: :text %>
<%= f.input_field :language %>
<%= f.input :premium, as: :boolean, inline_label: true, label: "Make this listing stand out", class: "form-control" %>
<%= f.button :submit, class: "btn btn-lg btn-primary pull-right" %>
<% end %>
This is what my companies/_form.html.erb looks like:
<%= simple_form_for(#company) do |f| %>
<%= f.input :name %>
<%= f.input :logo %>
<%= f.input :description %>
<%= f.input :city %>
<%= f.input :state %>
<%= f.input :country %>
<%= f.input :email %>
<%= f.button :submit %>
<% end %>
How can I combine them into 1 form, or some other unified workflow within 1 view where it works seamlessly to the user?
Edit 1
Based on Jay-Ar's answer, this is what is happening now.
When I select New Company in the Company Dropdown, it doesn't show the fields in the <fieldset>. I believe that's the case because there is no value=0 in the select tags rendered in the HTML, as can be seen in the screenshot below.
Edit 2
After attempting the latest update from Jay-Ar, the JS still doesn't work and the form is no longer hidden.
This is what it looks like on first load, and always:
Ideally I would like for this form not to show up until they have chosen "New Company" from the dropdown.
This is what the HTML looks like now:
The JS does appear in the source, so I know it is being loaded in the asset pipeline correctly.
UPDATED & TESTED WORKING
Now supports Turbolinks
views/jobs/_form.html.erb
<%= simple_form_for(#job) do |f| %>
<%# IMPORTANT: use `include_blank` below instead of `prompt` because prompt does not seem to work when updating, but only works when creating %>
<%= f.association :company, collection: [['New Company', nil]] + Company.pluck(:name, :id), include_blank: 'Please Select Company', input_html: { id: 'company-select' } %>
<fieldset id='job-fields'>
<%= f.simple_fields_for :company, #job.build_company do |ff| %>
<%= ff.input :name %>
<%= ff.input :logo %>
<%= ff.input :description %>
<%= ff.input :city %>
<%= ff.input :state %>
<%= ff.input :country %>
<%= ff.input :email %>
<% end %>
</fieldset>
<%= f.input_field :title %>
<%= f.input_field :salary %>
<%= f.input_field :description, as: :text %>
<%= f.input_field :apply_description, as: :text %>
<%= f.input_field :language %>
<%= f.input :premium, as: :boolean, inline_label: true, label: "Make this listing stand out", class: "form-control" %>
<%= f.button :submit, class: "btn btn-lg btn-primary pull-right" %>
<% end %>
JS
// for Rails 5, use turbolinks:load instead of page:change below
$(document).on('page:change', function(){
var companySelect = $('#company-select');
var jobFields = $('#job-fields');
companySelect.change(function(){
// if selected option is the second option
if ($(this).find('option:selected').index() == 1)
jobFields.show().find(':input').prop('disabled', false);
else
jobFields.hide().find(':input').prop('disabled', true);
})
// call change immediately so this still works when already updating and not just creating.
companySelect.change();
})
controllers/jobs_controller.rb
class JobsController < ApplicationController
...
def create
#job = Job.new(job_params)
...
end
def update
#job = Job.find(params[:id]) # not needed if using before_action #set_job
if #job.update(job_params)
...
end
private
def job_params
params.require(:job).permit(:id, :title, :salary, :description, :apply_description, :language, :premium, :company_id, company_attributes: [:name, :logo, :description, :city, :state, :country, :email]
end
end
models/job.rb
class Job < ActiveRecord::Base
accepts_nested_attributes_for :company
validates :company, presence: true
...
end
You should get something like the following:
On fresh load:
After selecting 'New Company' option':
You should use nested model forms, I suggest you watch this video it has a very detailed explanation of the steps to do it for a question / answer models but its the same thing.
I have two Model Product and Supplier.
Product has_one supplier.
how to Build a Seach form inside a form.
,I want to Search Supplier and Put that id into My Product form.
Currently my code look like this.
<%= f.text_field :product_code, class: 'form-control'%>
<%= f.text_field :product_name, class: 'form-control'%>
<%= f.text_field :supplier_id, class: 'form-control'%>
<%= render :partial => 'supplier_search' %>
<%= f.submit 'Save', :class=>'button add'%>
Render suplier_search partial after submit button of product form.
<%= f.text_field :code, class: 'form-control'%>
<%= f.text_field :supplier_id, class: 'form-control'%>
<%= f.submit 'Save', :class=>'button add'%>
<%= render :partial => 'supplier_search' %>
Or
Don't have form in suplier_search partial. Just put input field or whatever you want, there and submit those fields via Ajax and get your required results in product form.
if its not partialing for <%= render :partial => 'supplier_search' %>
then use
"your views folder name/supplier_search" %>