Rails Updating Global Variable - ruby-on-rails

I'm trying to set a site-wide global variable in my Rails application using the form at the following site:
http://spin360-staging.herokuapp.com/printer
The results should be visible here:
http://spin360-staging.herokuapp.com/printer.json
However, if you refresh the json, you'll see that it doesn't seem persistent - it seems to change values when you refresh the page.
Here's what the form looks like:
<%= form_tag edit_printer_path do %>
<%= label_tag "Type" %>
<%= select_tag(:type, options_for_select(
[
['Set', "set"],
['Spin', "spin"]
], 1
)) %>
<%= label_tag "ID" %>
<%= text_field_tag :print_id %>
<%= label_tag "Format" %>
<%= select_tag(:layout, options_for_select(
[
['Receipt', "receipt"],
['Filmstrip', "filmstrip"],
['Flipbook', "flipbook"]
], 1
)) %>
<%= submit_tag "Submit", :class => "submit_button" %>
And the controller method:
def edit_printer
Rails.application.config.print_type = params[:type]
Rails.application.config.print_id = params[:print_id]
Rails.application.config.print_layout = params[:layout]
redirect_to(:back)
end
And finally, the JSON template (printer.json.erb)
{
"print": [{"type":"<%= Rails.application.config.print_type %>","id":<%= Rails.application.config.print_id %>,"layout": "<%= Rails.application.config.print_layout %>"}]
}

Related

Ransack for two different models

I am getting my heads around many things RoR - by developing a small movie database that has actors and movies.
I successfully setup Ransack to do two things for me:
Following Chris' great tutorial on autocomplete https://gorails.com/episodes/global-autocomplete-search I now have a working search field in my navbar with dropdown, autoselect etc - all works like charm :)
Set up a separate "moviesearch" method in my mainpage controller - with corresponding form etc - working great too.
Code of moviesearch method:
#q = Movie.ransack(params[:q]
#movies_search = #q.result(distinct: true).includes(:genres, :actors)
However I now try to setup an "actorsearch" method - also in my mainpage controller - with corresponding form etc... doesn't work. It does reply the right index page (view/actors/index.html.erb), but it doesn't pass any of the search parameters on.
Code of actorsearch method:
#q = Actor.ransack(params[:q])
#actors_search = #q.result(distinct: true).includes(:movies, :actor_attributes)
When I check the console - the moviesearch method triggers something like this:
SELECT COUNT(*) FROM (SELECT DISTINCT `movies`.* FROM `movies` WHERE `movies`.`title` LIKE '%lord%' ORDER BY `movies`.`sort_title` ASC) subquery_for_count
But the actorsearch triggers only:
SELECT COUNT(*) FROM `actors`
Not sure what's wrong...
Update: Here's the form code for the actor search - omitted all html-only stuff (div's etc)
<%= search_form_for #q do |f| %>
<%= f.label :display_name_cont, 'Name contains', class: 'form-label mb-0' %>
<%= f.search_field :display_name_cont, class: 'form-control' %>
<%= f.label :gender_eq, 'Gender is', class: 'form-label mb-0' %><%= f.search_field :gender_eq, class: 'form-control' %>
<%= f.label :actor_attributes_id_in, 'Attributes', class: 'form-label mb-0' %>
<%= f.collection_select(:actor_attributes_id_in, ActorAttribute.order(:name), :id, :name, {}, { class: 'form-select bootstrap', id: 'attribute-select', multiple: true } ) %>
<%= f.label :born_gteq, 'Born between', class: 'form-label mb-0' %><%= f.number_field :born_gteq, class: 'form-control' %></div>
<%= f.label :born_lteq, 'and', class: 'form-label mb-0' %>
<%= f.number_field :born_lteq, class: 'form-control' %>
<%= f.label :rating_gteq, 'Actor rated at least', class: 'form-label mb-0' %>
<%= f.number_field :rating_gteq, class: 'form-control' %>
<%= f.label :movies_id_eq, 'Movies starring in', class: 'form-label mb-0' %>
<%= f.collection_select(:movies_id_in, Movie.order(:sort_title), :id, :title, {}, { class: 'form-select bootstrap', id: 'movie-select', multiple: true } ) %>
<%= f.submit class: 'btn btn-outline-primary' %>
<% end %>
And I didn't quite know what you mean with "puts params[:q] into the controller. I have already tried to check the params with outputting debug(params[:q]) in various places - for example, in my actors index page, it shows like this:
parameters: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
display_name_cont: john
gender_eq: ''
actor_attributes_id_in:
- ''
born_gteq: ''
born_lteq: ''
rating_gteq: ''
movies_id_in:
- ''
permitted: false
...which looks pretty similar to the output for movies... at least I couldn't spot any immediate issue here?

Ruby on Rails : Advance search, undefined method [ ] for nil:Nil class

I am very new to rails and try to make an advance search form which takes two values 'blood_group' and 'area' and based upon that search the records from the database are fetched and will display on the same page (find.html.erb)
I have tried something in find.html.erb but there is an error occurred 'undefined method [] for nil:Nil class' where my search form exists. please help to get out of this error.
Here is my search form in find.html.erb
<%= form_tag find_path , method: :get do %>
<p> <%= label_tag :blood_group %><br />
<%= select_tag (:blood_group), options_for_select(%w[ A+ B+ O+ AB+ A- B- O- AB-]), params[:blood_group] %> </p>
<p> <%= label_tag :area %><br />
<%= select_tag (:area), options_for_select(%w[Indore Vijay_Nagar Bhawar_Kuwa Rajendra_Nagar Geeta_Bhawan Aerodram Tejaji_Nagar Raj_Mohalla Rajwada Chandan_Nagar Gandhi_Nagar Arvindo MY Bombay_Hospital]) , params[:area] %> </p>
<%= submit_tag "Search" , class: "btn btn-primary" , name: nil %>
<% end %>
The error is at the lines where I used select tag.
below is my find action in Donor controller
def find
#donors = Donor.search(params[:blood_group], params[:area]).all
end
And Donor.rb is as follows
class Donor < ActiveRecord::Base
def self.search(blood_group, area)
return all unless blood_group.present? || area.present?
where(['blood_group LIKE ? AND area LIKE ?', "%#{blood_group}%", "%#{area}%"])
end
end
Your search form must be like this:
<%= form_tag find_path , method: :get, remote: true do %>
<p> <%= label_tag :blood_group %><br />
<%= select_tag (:blood_group), options_for_select(%w[ A+ B+ O+ AB+ A- B- O- AB-]) %> </p>
<p> <%= label_tag :area %><br />
<%= select_tag (:area), options_for_select(%w[Indore Vijay_Nagar Bhawar_Kuwa Rajendra_Nagar Geeta_Bhawan Aerodram Tejaji_Nagar Raj_Mohalla Rajwada Chandan_Nagar Gandhi_Nagar Arvindo MY Bombay_Hospital]) %> </p>
<%= submit_tag "Search" , class: "btn btn-primary" , name: nil %>
<% end %>
Modify your controller as:
def find
#donors = Donor.search(params[:blood_group], params[:area]).all
respond_to do |format|
format.js
end
end
Then add find.js.erb to show the searched content.

Rails 4 dropdown menu pulling from database

I am trying to implement a dropdown menu to display all the names I have in my database. I tried unsuccessfully the following code:
<%= form_for #airline, remote: true do |f| %>
<%= f.select :name, [#airlines.names] %>
<%= f.submit %>
<% end %>
Controller:
def index
#airlines = Airline.all
#airline = Airline.new
end
I assume the solution is quite straight forward but I couldn't find it.
This should do
<%= f.select(:name, #airlines.collect { |airline| [airline.name,airline.id] }, {:include_blank => 'Choose 1'},:class=>"class_name") %>

Generate parameters in ruby on rails

I want an update method in the controller to get parameters in a specific format. The current format by which I am getting the parameters is
Parameters: {"utf8"=>"✓", "authenticity_token"=>"temp", "performance_areas"=>{"0"=>{"performance_area"=>"New item"}, "1"=>{"performance_area"=>"Delete Item"}, "commit"=>"Submit"}
This is being generated as a name in my text field with 0 and 1 indicating the areas index. I want some additional parameters in this in the following format:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"temp", "performance_areas"=>{"0"=>{id: 1, description: "This is a test","performance_area"=>"New item"}, "1"=>{id: 2, description: "This is a test2","performance_area"=>"Delete Item"}, "commit"=>"Submit"}
Any pointers how to get the parameters in the way I want. I do have performance_areas that has the id and description in it. Do let me know if any further clarification is required.
EDIT: Form code (just pasting the critical section of the long code)
<%= form_tag({:performance => :update}, :method => :put) do %>
<% #performance_areas.each_with_index do |performance_area, i| %>
<%= hidden_field_tag :performance_area, performance_area, :name => "performance_areas[#{i}][performance_area]" %>
<%= submit_tag %>
I think you want this, I am assuming that both id and description are in performance_area object
<%= form_tag({:performance => :update}, :method => :put) do %>
<% #performance_areas.each_with_index do |performance_area, i| %>
<%= hidden_field_tag :id, performance_area.id, :name => "performance_areas[#{i}][id]" %>
<%= hidden_field_tag :description, performance_area.description, :name => "performance_areas[#{i}][description]" %>
<%= hidden_field_tag :performance_area, performance_area, :name => "performance_areas[#{i}][performance_area]" %>
<%= submit_tag %>
<% end %>

Rails check_box_tag within form_for

Is it possible to pass the value of checked check_box_tags within a form_for in Rails inside a hash?
Here is a very generic, basic version of the form:
<%= form_for(:object, :url => { :action => 'create', :id => params[:id]}) do |f| %>
<p>Field_a: <%= f.text_field(:field_a) %></p>
<p>Field_b: <%= f.text_field(:field_b) %></p>
<p>Field_c: <%= f.text_field(:field_c) %></p>
<p>Check boxes:</p>
<% check_box_choices_object_array.each do |s| %>
<%= check_box_tag(s.name, 1, false) %>
<%= .name %><br />
<% end %>
<%= submit_tag("Create") %>
<% end %>
Outputs roughly:
Field_a ___________________
Field_b ___________________
Field_c ___________________
Check boxes:
[] box_a
[] box_b
[] box_c
[] box_d
[] box_e
[] box_f
[] box_g
My problem is that since the available check boxes aren't actual fields in the object's table in the database (i.e. I'm not using check_box(:field) in the form), each checked check box gets passed as an individual parameter (i.e. "box_a" => "1", "box_b" => "1", "box_e" => "1"). I would like them to be passed as such:
:checked_boxes => {"box_a" => "1", "box_b" => "1", "box_e" => "1"}
This way, I can access them easily with params[:checked_boxes].
How do I do this, or, better yet, is there a better solution (I'm new to rails)?
I think you'd get the results you want if you wrap the checkboxes iterator in a fields_for :checked_boxes tag - or at least get you close to the results you want.
<%= form_for(:object, :url => { :action => 'create', :id => params[:id]}) do |f| %>
<p>Field_a: <%= f.text_field(:field_a) %></p>
<p>Field_b: <%= f.text_field(:field_b) %></p>
<p>Field_c: <%= f.text_field(:field_c) %></p>
<p>Check boxes:</p>
<%= f.fields_for :checked_boxes do |cb| %>
<% check_box_choices_object_array.each do |s| %>
<%= cb.check_box(s.name, 1, false) %>
<%= .name %><br />
<% end %>
<% end %>
<%= submit_tag("Create") %>
<% end %>
you can deal with no database attributes and models using attr_accessor
class Thing < ActiveRecord::Base
attr_accessible :name
attr_accessor :box_a, :box_b, :box_c
end
This way you can call these attributes in your form.

Resources