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") %>
Related
I am trying to make the following:
Any solution to update the database with the value and with the checkbox without using JavaScript with a click on or click off?
<%= form_for :act_as do |f| %>
<%= f.check_box(:suspend_flag, { checked: (ActAs.find_by(ModifiedID:#misc_data[:user]).SuspendFlag ? false :true), class: 'abcd' }, 0, 1) %>
<%= f.submit %>
<% end %>
This is a field that lets people select an article, that they want to connect with the present article to:
<%= form_for(Relation.create, remote: true) do |f| %>
<%= f.hidden_field :dependent_id, :value => #article.id %>
<%= f.collection_select :dependency_id, Article.all.reject{|x| (x.dependency_hack.map{|x| x.dependent_id}.include? #article.id) &&
(if current_user then x.dependency_hack.map{|x| x.user}.include? current_user end) ||
x.id == #article.id}, :id, :title, {} %>
<%= f.submit "Add suggestion" %>
<% end %>
But this loads all article titles, which is wasteful and slow. Instead, titles should be loaded dynamically, after the user types a few characters.
I'm new on rails. I work on a project, and I try to add physical address to my users. I want that the address can be help-complete with Google map for later exploitation. I find the jt-rails-address which look like perfect for my project. But I can't implement it. I need complete address (street, zip code, city & country).
add_address_to_users.rb :
class AddAddressToUsers < ActiveRecord::Migration[5.1]
def change
add_column :users, :address, :address
end
end
form edit.html.erb :
<div class="col-md-6 col-md-offset-3">
<%= form_for (#user), :html => { :multipart => true } do |f| %>
<%= render 'shared/error_messages' %>
<%= f.label :phone, "Téléphone :" %>
<%= f.phone_field :phone, class: 'form-control' %>
<%= f.label :address, "Addresse :" %>
<div class="jt-address-autocomplete">
<!-- This field is used to search the address on Google Maps -->
<%= f.text_field :address, class: 'jt-address-search' %>
<!-- All fields are hidden because the javascript will set their value automatically -->
<% for attr in JT::Rails::Address.fields %>
<%= f.hidden_field "address_#{street}", class: "jt-address-field-#{street}" %>
<% end %>
<% for attr in JT::Rails::Address.fields %>
<%= f.hidden_field "address_#{zip_code}", class: "jt-address-field-#{zip_code}" %>
<% end %>
<% for attr in JT::Rails::Address.fields %>
<%= f.hidden_field "address_#{city}", class: "jt-address-field-#{city}" %>
<% end %>
<% for attr in JT::Rails::Address.fields %>
<%= f.hidden_field "address_#{country}", class: "jt-address-field-#{country}" %>
<% end %>
</div>
<%= f.submit "Enregistrer les changements", class: "btn btn-primary" %>
<% end %>
</div>
application.js :
// This function is call when Google Maps is loaded
window.googleMapInitialize = function(){
// Simple usage
$('.jt-address-autocomplete').jt_address();
};
I have already put :address in my user_params in the users controller and has_address :address in the user model
I also have put my Google Api.
My actual error:
undefined local variable or method `street' for #<#Class:0x00007fe91ec7f768:0x00007fe91dfaefc0>
Thanks in advance for your help.
No more error but a bug, maybe because of Google Map, I don't know how
to resolve it, I can't write the address and there is an error message:
problem
Here the code of the inspector:
First, you should add fields like in documentation or scope them using select, now you have code that will create duplicated hidden fields.
You should check from rails console that method address is available on User instance of model.` Maybe you forgot to run migration or didn't restart server after installing this gem as this is js gem and will need restart.
Hope this answers your questions. Add comment to this answer if problem is still there.
I know this question has been answered before, but none of the methods had worked for me.
I have a view with a select_tag that loads options from my database. <%= select_tag :nom, options_from_collection_for_select(Usuario.all, :id, :tienda), prompt: "Seleccionar tienda" %>
then, I use a link_to <%= link_to("Cargar", :action => 'rel') %><br>to load the query on my controller
def rel
#nom = params[:nom]
#tie = Av.find_by_sql(["SELECT * FROM avm.avs where usuario_id in(select id from avm.usuarios where tienda = ?)", #nom])
render('rel')
The problem is that when I select any value on my select_tag it does not pass that value and sets the value to nil...
I also used a collection_select and doesn't work either. <%= collection_select(:tienda, :tienda, Usuario.all, :id, :tienda)%>
I really broke my head off trying to figure out why. Thanks in advance!
Use form_tag:
<%= form_tag action: rel do %>
<%= select_tag :nom, options_from_collection_for_select(Usuario.all, :id, :tienda), prompt: "Seleccionar tienda" %>
<br>
<%= link_to("Cargar", '#', onclick: 'this.parentNode.submit(); return false') %>
<% end %>
Hint: replace find_by_sql by Arel:
#tie = Av.where("usuario_id IN (#{Usuario.select('id').where('tienda = ?', #nom).to_sql})")
I think you should put the select_tag in a form_tag block, and use submit_tag instead of link_to. If you just use a link no parameters will be passed to the controller (hence you get nil).
Something like:
<%= form_tag action: 'rel' do %>
<%= select_tag :nom, options_from_collection_for_select(Usuario.all, :id, :tienda), prompt: "Seleccionar tienda" %>
<%= submit_tag 'Cargar' %>
<% end %>
Just be mindful that this approach is not safe as there is no authenticity token verification.
I would like to rewrite a form which is used to update a record on a database.
I want to update the form so that the form input does not show the record, as
the record is outputted by the line
<%= q.object.content %>.
I want the
form input not to display the record, and I want that the record is updated
when the input field is edited, and is not edited when it is left blank.
I am new at working with forms and don't know the best way to achieve this.
Can anyone provide any help on achieving this ? Below is the current form. Any help would be appreciated.
<%= semantic_form_for #bunchOfThings do |f| %>
<%= f.inputs do %>
<%= f.semantic_fields_for :aThing, #aThing do |q| %>
<%= q.object.content %>
<%= q.input :content, label: "A Thing: #{q.object.content}" %>
<% end %>
<% end %>
<%= f.action :submit , label: t('Some Text'), button_html: { class: 'btn btn-primary' } %>
<% end %>
You can manually set the default value of a field to an empty string by changing this line:
<%= q.input :content, label: "A Thing: #{q.object.content}" %>
To this:
<%= q.input :content, label: "A Thing: #{q.object.content}", input_html: {value:''} %>
You would also need to filter out blank fields on the backend within the update controller method. Something like this:
def update
filtered_params = permitted_record_params
filtered_params.keep_if{|k,v| !v.blank? }
record.update(filtered_params)
...
end
Where of course the permitted_record_params method returns your permitted params hash.