rails4-autocomplete with belongs to association - ruby-on-rails-4

I want to implement autocomplete via rails4-autocomplete
Rails 4.2.4
Here is the controller
app/controllers/samples_controller.rb
class SamplesController < ApplicationController
autocomplete :patient, :code
Here is the route file,
config/routes.rb
resources :samples do
get :autocomplete_patient_code, on: :collection
end
And that's the view
app/views/samples/_form.html.erb
<div class="field">
<%= f.label :patient_code %><br>
<%= f.autocomplete_field :patient_id, autocomplete_patient_code_samples_path %>
</div>
With this code I mange to get the autocomplete
However I get invalid foregin key error when try to save the sample that's because the patient's code is passed to the foregin key instead of ID. How do I fix this?
Here is the request Parameters:
{"utf8"=>"✓",
"authenticity_token"=>"blabla",
"sample"=>{"patient_id"=>"A123",
"commit"=>"Create Sample"}
Get "/samples/autocomplete_patient_code?term=A12" returns
{"id":"15","label":"A123","value":"A123"}]

After reading the GitHub documentation of rails4-autocomplete, I devised the following solution:
Add attr_accessor :patient_name to your Sample model and modify the form as follows:
...
<%= f.autocomplete_field :patient_name, autocomplete_patient_code_samples_path, id_element: '#patient_id' %>
<%= f.hidden_field :patient_id, id: 'patient_id' %>
...
With this change whenever you select any patient name, that patient's ID will be updated on the hidden field and it will be submitted as patient_id.
Hope this solves your problem.
Source: Github

Related

Select_tag wont dispay anything

I have the following code
<%= form_tag('/update', method: :post) do %>
<%= select_tag :role, UserSomething.roles.keys.map {|role| [role.titleize,role]} %>
Role is an enum which text values have to be displayed in the drop down menu, and on form submit, i have to send the index of selected enum to some controller. I don't know how to set select_tag propertly.
I would use the helper options_for_select to map the array you get from UserSomething.roles.keys.map to a list of options for the select. I don't think out the select_tag method handles an array of the box, it needs a list of option tags. See the docs here.
<%= form_tag('/update', method: :post) do %>
<%= select_tag :role, options_for_select(UserSomething.roles.keys.map {|role| [role.titleize,role]}) %>
<% end %>

autocomplete field is empty when editing form

I have a sample model in which belongs_to a patient model.Using rails-jquery-autocomplete I have managed to implement an autocomplete field where one can search patient's code and it works well. However when editing the form, the patient code is empty on the form.
How should I fix it?
App/views/sample/_form.html.erb
<div class="field">
<%= f.label :patient_code %><br>
<%= f.hidden_field :patient_id, id: 'patient_id' %>
<%= f.autocomplete_field :patient_code, autocomplete_patient_code_samples_path, id_element: '#patient_id' %>
</div>
I faced the same issue while editing the saved model, a quick workaround would be as follows:
...
<%= f.autocomplete_field :patient_code, autocomplete_patient_code_samples_path, id_element: '#patient_id', value: (!#sample.new_record?)? #sample.patient.code : '' %>
...
Assuming that the instance variable is #sample. Modify it according to your scenario. Cheers!

(Rails 4) Acts_as_taggable_on: using a model other than "Tag"

I am trying to implement the acts_as_taggable_on gem. In my set up I have a Model called Discipline which is pre-populated with about 40 names.
I also have a Model Micropost which I want to tag - using a select box containing all the names in the disciplines database. I thought that I could call the acts_as_taggable_on the Model I wanted - in this case Disciplines but its not working with my current set up.
class Micropost < ActiveRecord::Base
acts_as_taggable
acts_as_taggable_on :disciplines
end
Here is the form......
<%= simple_form_for(#micropost) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.input :tag_list, :as => :select,
:multiple => :true,
:collection => ActsAsTaggableOn::Tag.pluck(:name) %>
<%= f.text_area :content, placeholder: "What is your question?", :style => "height:75px;" %>
<%= f.submit "Post", class: "btn btn-primary" %>
<% end %>
I can tell from the documentation that there is a way to do this....but I guess I am just not getting it. Any suggestions?
I don't think you can use acts_as_taggable_on using a model other than the default Tag and Taggings models.
Alternative Approach #1
Seed your database with the pre-populated 40 Tags containing your discipline names.
Alternative Approach #2
Use bitmask_attributes for your 40 disciplines.
For example, in my application I have:
bitmask :instruments, as: [:others, :guitar, :piano, :bass, :mandolin, :banjo,
:ukulele, :violin, :flute, :harmonica, :trombone,
:trumpet, :clarinet, :saxophone, :viola, :oboe,
:cello, :bassoon, :organ, :harp, :accordion, :lute,
:tuba, :ocarina], null: false

Rails object method returning full array

I'm having an issue with an association. I've got an Employee model that belongs_to a Role model. When I try to display the association, I get the full array displayed back.
Here's the show action from my Employee controller. As you can see, I've tried a few different methods to make the proper association in the first place:
def show
#employee = Employee.find(params[:id])
# #role = Role.where(:id => #employee)
# #role = Role.find_by_sql("select roles.role_title from roles where roles.id in (select role_id from employees where role_id='1')")
#role = Role.where(id: #employee)
end
And here's the view:
<p>
<strong>Role:</strong>
<%= #role.each do |r|
r.role_title
end %>
</p>
My output comes back as:
Role: [#<Role id: 3, role_title: "Support Engineer", created_at: "2014-08-20 16:09:22", updated_at: "2014-08-20 16:09:22">]
What am I missing here?
You need to actually iterate and display something for each role.
<%= %> means "display the result of the expression", which in your case, is an each.
each returns the collection you were iterating over. You want something closer to:
<% #role.each do |r| %>
<%= r.role_title %><br/>
<% end %>
Although it obviously depends on what you actually want to display, for example:
<%= #role.collect(&:role_title).join(', ') %>
Unrelated: I might argue that Role#role_title is redundant and Role#title would be sufficient.
If the employee belongs_to a role there is only one role for each employee.
You can retrieve it as easily as specifying...
#employee.role
but if you insist on constructing a separate retrieval then
#role = Role.where(id: #employee.role_id).first
EDIT
So talking about the views... if there's only one #role you don't need to iterate through an array...
<p>
<strong>Role:</strong> <%= #role.role_title %>
</p>
You're seeing an array because the where returns an array, you could bypass that with...
#role = Role.where(id: #employee).first
As Dave Newton pointed out, if it really was an array you'd need to do...
<p>
<strong>Role:</strong>
<% #role.each do |r| %>
<%= r.role_title %>
<% end %>
</p>

RubyonRails Guide confusion over link creation in Ruby

I am a beginner to Rails. Was working along with Railsguide for Rails 4.
One confusion i have is:
for adding a link , somewhere it is written like:
<h1>Hello, Rails!</h1>
<%= link_to "My Blog", controller: "posts" %>
whereas somewhere its like
<%= link_to 'New post', new_post_path %>
Please clarify the difference.
Good question. Both of these are link helpers, and both are resource-oriented (check out the Rails UrlHelper docs for more information).
The first one will render a link that is associated with the particular controller:
<%= link_to "My Blog", controller: "posts" %>
<a href='/posts'>
The second will render the Rails path specific to creating a new Post object (this is also resource-oriented). Check out section 2.3 in the Rails routing guide:
<%= link_to 'New post', new_post_path %>
<a href='/posts/new'>