Rails FormBuilder - How do use Select with Grouped Options? - ruby-on-rails-3

I have a Rails form, where I am trying to insert a Select formfield .
My current code runs like this -
<select id="selectservice" name="service">
<% #categories.each do |category| %>
<% #services= category.services %>
<% #services.each do |service| %>
<option value="<%= service.id %>"><%= service.name %></option>
<% end %>
<% end %>
</select>
Now I want to convert this into a Formbuilder style as the rest of the form is in that style . And also I will be able to insert the variable which is missing here . How do I go about inserting the option tags ?
<%= form_for #appointment do |f| %>
<%= f.select :service_id, :name=>"service" %>
##How do I insert the Option tags here ?
<%= end %>

I think I found the answer . This can be achieved by Grouped Options for Select
<%= f.grouped_collection_select :service_id,
#categories, :services, :name,
:id, :name , :id=>"selectservice"
%>

Related

Ruby on Rails - simple_form fields_for create different index

I have a question about updating one-to-many fields using simple_form field_for method
I have 2 models, Company and Clients, which has a one-to-many relationship.
I displayed clients using field_for, but for UI reason, I had to called it twice.
But for some reason, the index of the input fields was given a different value. Below is my code
<%= simple_form_for #company do |f| %>
<table>
<tr>
<td>
<%= f.input :name, label: 'Company name: ' %>
<%= f.simple_fields_for :clients do |client| %>
<%= client.input :name, label: 'Client names: ' %>
<% end %>
<%= f.input :info, label: 'Company info: ' %>
</td>
<td class="span2 clients_desc">
<%= f.simple_fields_for :clients do |client| %>
<%= client.input :description, label: 'Client description: ' %>
<% end %>
</td>
</tr>
</table>
<% end %>
Say if I had 3 clients, the output for the name of the input fields became
company[client_attributes][0][name], company[client_attributes][1][name], company[client_attributes][2][name]
and
company[client_attributes][3][description], company[client_attributes][4][description], company[client_attributes][5][description]
This result in duplicating clients during store. How do I solve this?
An easy workaround would be "caching" the form fields like so:
# ...
<%= f.simple_fields_for :clients do |client| %>
<%= client.input :name, label: 'Client names: ' %>
<% client_description_input = client.input :description, label: 'Client description: ' %>
<% end %>
# ...
<%= client_description_input %>
# ...

undefined method `model_name' for NilClass:Class in edit form

I have a rails app where I can create and edit records. I've created a form to enter data which works fine when I use the new/create actions. It will create a record no problem. But when I hit the edit action it gives me an undefined method 'model_name' for NilClass:Class.
I'm not sure what this means. Can someone give me a hand?
Form:
<%= form_for(#patient) do |f| %>
<%= f.label :Patient_Last_Name %>
<%= f.text_field :patient_last %>
<%= f.label :Patient_First_Name %>
<%= f.text_field :patient_first %>
<%= f.label :Patient_DOB %>
<%= f.date_select :patient_dob %>
<%= f.label :Primary_Diagnosis %>
<%= f.collection_select(:diagnosis_id, Diagnosis.all, :id, :diagnosis_name)%></br>
<%= f.label :Primary_Physician %>
<%= f.collection_select(:physician_id, Physician.all, :id, :physician_name)%></br>
<%= f.button :submit %>
<% end %>
View Code:
<td><%= link_to 'Edit', edit_patient_path(patient), :class => 'btn btn-close btn-mini'%></td>
Controller Code:
def edit
#patient = Patient.find(params[:id])
end
Edit view:
<%= render 'form' %>
When I remove the partial render from the form, the URL will go to the correct route/url. But I keep getting that error when the form partial is rendered.
There was an extra end statement in my controller which was cutting off half of the class which included the edit action. This was not allowing me to use the edit action. Once this typo was fixed things started working normally.
Sorry for the confusion.

Rails 3 using ransack to search and sort and trying to limit options in sort select

I have a page in my rails application that shows a list of listings (paginated using will_paginate). I am trying to find a good gem to implement searching and sorting with the list on this page. The Ransack gem looked like what i needed but i'm running into an issue with it. This is what my code looks like right now with the Ransack gem.
My model (didn't include the whole model, didn't want the validations and extra code to get in the way):
UNRANSACKABLE_ATTRIBUTES = ["id", "state", "show_email", "email", "show_phones", "primary_phone", "secondary_phone", "updated_at", "user_id"]
def self.ransackable_attributes auth_object = nil
(column_names - UNRANSACKABLE_ATTRIBUTES) + _ransackers.keys
end
My controller action:
def index
#search = Listing.search(params[:q])
#listings = #search.result.paginate(page: params[:page], per_page: 20, joins: "LEFT OUTER JOIN attachments ON listings.id = attachments.attachable_id", select: "listings.*, COUNT(attachments.id) AS photos", group: "listings.id", order: "listings.created_at DESC, listings.title ASC, listings.id DESC")
#search.build_sort if #search.sorts.empty?
end
My search form on the page:
<div class="advanced-search">
<%= search_form_for #search do |f| %>
<div class="search-by">
<div class="search-field">
<%= f.label :title_cont, "Title Contains" %>
<%= f.text_field :title_cont %>
</div>
<div class="search-field">
<%= f.label :description_cont, "Description Contains" %>
<%= f.text_field :description_cont %>
</div>
<div class="search-field">
<%= f.label :price_gteq, "Price Between" %>
<%= f.text_field :price_gteq %>
<%= f.label :price_lteq, "and" %>
<%= f.text_field :price_lteq %>
</div>
</div>
<div class="sort-by">
<%= f.sort_fields do |s| %>
<%= s.sort_select %>
<% end %>
</div>
<div class="search-actions">
<%= f.submit "Search" %>
</div>
<% end %>
</div>
My route:
resources :listings do
collection do
match 'search' => 'listings#index', via: [:get], as: :search
end
end
With everything i have in place now i have been able to get the application to successfully search and sort the listings.
My question: The sort_select field is showing everything option that is "ransackable". Is there a way to make one attribute available for the searching, but not the sorting? For example i want the ability to search on the description attribute but it doesn't make any sense to include that attribute in the sorting. How can i use an attribute in one and not the other? Thanks for any help and let me know if my question is not clear enough.
Couldn't figure out how to get the values i wanted into the dropdown so i just used links instead. Here's what i ended up with for the sorting.
Sort by <%= sort_link #search, :title %> | <%= sort_link #search, :price %> | <%= sort_link #search, :created_at, "Date Listed" %>

Rails 3 - Ransack - check_box_tag

Listing Model - belongs_to :area
Area Model - has_many :listings
I'm trying to implement a search using Ransack with check boxes; where user checks selected areas, search returns all the listings of the areas selected.
<%= search_form_for #search do |f| %>
<% areas = Area.all %>
<% areas.each do |area| %>
<%= check_box_tag('q[area_id_eq][]', area.id) %>
<%= area.location%>
<% end%>
<%= f.submit "SEARCH" %>
<% end %>
Console output:
Parameters: {"utf8"=>"✓", "q"=>{"area_id_eq"=>["1", "2"]}, "commit"=>"SEARCH"}
Completed 500 Internal Server Error in 4ms
NoMethodError - undefined method `to_i' for ["1", "2"]:Array:
Just not sure how to implement it to accept multiple check box values.
Instead of using "area_id_eq", use "area_id_any". You'll also want to check to make sure that your parameters are selected:
<%= search_form_for #search do |f| %>
<% areas = Area.all %>
<% areas.each do |area| %>
<%= check_box_tag('q[area_id_eq_any][]', area.id, (params[:q][area_id_eq_any].include? area.id.to_s) ? true : false ) %>
<%= area.location%>
<% end%>
<%= f.submit "SEARCH" %>
<% end %>

I have a two form with a single submit button. Then how to i need to save the values of those form values?

form with the single submit. How to store that values in the data base
<%= form_for (#movie) do |f| %>
//Movie
<%= f.label :moviename,"Movie Name:"%>
<%= f.text_field :moviename%>
//Releases
<%= f.fields_for :release do |release_fields| %>
<%= release_fields.label :theatre,"Theatre :"%>
<%= release_fields.text_field:theatre%>
<%= release_fields.label :city,"City :"%>
<%= release_fields.text_field:city%>
<%= release_fields.label :releasedate,"Release Date :"%>
<%= release_fields.text_field:releasedate%>
//Submit:
<%= f.submit "Save The Movie"%>
<% end %>
<% end %>
this is the form with the single submit button.There an error it showing the unknown attribute release can any on help with is issue Please
fields_for is expecting to access #movie.release, does that exist?