These are the fields I have in my view, the autocomplete on the partcode brings up a list of partcodes from a partcode table. Depending on what partcode is chosen, i want it to dynamically update the description with the description from the partcode table.
<div class="field">
<%= f.label "partcode" %><br />
<%=f.autocomplete_field :partcode, "/goods_ins/1/autocomplete_partcode_partcode" %>
</div>
<div class="field">
<%= f.label "description" %><br />
<%= f.text_field :description, :autocomplete => :off %>
</div>
This is in my routes:
resources :goods_ins do
get :autocomplete_partcode_partcode, :collection => {:view => :get}
end
And my controller:
autocomplete :partcode, :partcode, :extra_data => [:description]
Use following way,
First give id to your text field as below
<%= f.text_field :description,:autocomplete=>:off, :id_element=>'#part_desc'%>
Now set update_elements property with above element id as below
<%=f.autocomplete_field :partcode, "/goods_ins/1/autocomplete_partcode_partcode",:update_elements=>{:id =>'#part_code', :description=>'#part_desc'} %>
Related
I have the following (and working) dynamic menu / dropdown which allows you to select a property type and then a property subtype with a regular rails form:
properties.js.coffee
jQuery ->
prop_sub_types = $('#property_prop_sub_type_id').html()
$('#property_prop_type_id').change ->
prop_type = $('#property_prop_type_id :selected').text()
escaped_prop_type = prop_type.replace(/([ #;&,.+*~\':"!^$[\]()=>|\/#])/g, '\\$1')
options = $(prop_sub_types).filter("optgroup[label='#{escaped_prop_type}']").html()
if options
$('#property_prop_sub_type_id').html(options)
else
$('#property_prop_sub_type_id').empty()
_form.html.erb
<%= form_for(#property) do |f| %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :prop_type_id, 'Property Type' %><br />
<%= f.collection_select :prop_type_id, PropType.order(:name), :id, :name, :prompt => "-- Select Property Type --" %>
</div>
<div class="field">
<%= f.label :prop_sub_type_id, 'Property Subtype' %><br />
<%= f.grouped_collection_select :prop_sub_type_id, PropType.order(:name), :prop_sub_types, :name, :id, :name, :prompt => "-- Select Property Subtype --" %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
This works fine. However, I'd like to integrate this into a larger app that is already set up with the simple form gem. I'm also using twitter bootstrap via bootstrap-sass.
The closest I can get in my form is:
<div class="field">
<%= f.association :prop_type, :input_html => { :id => "prop_type_id", :class => "span5" }, :prompt => "-- Select Property Type --" %>
<%= f.association :prop_sub_type, :input_html => { :id => "prop_sub_type_id", :class => "span5" }, :prompt => "-- Select Property Subtype --" %>
</div>
Note: I had to change :prop_type_id to :prop_type to keep the app from throwing errors.
But this is not working - the second drop down won't map to the first. I a doing something wrong in my java/coffeescript? Is there such a thing as 'grouped_association' or something along those lines for the second dropdown?
Is this even doable, or should I convert the entire form back to the standard rails format?
UPDATE
I was able to get it to work but sticking the erb into divs as follows:
<div class="field">
<%= f.collection_select :prop_type_id, PropType.order(:name), :id, :name, :prompt => "-- Select Property Type --" %>
</div>
<div class="field">
<%= f.grouped_collection_select :prop_sub_type_id, PropType.order(:name), :prop_sub_types, :name, :id, :name, :prompt => "-- Select Property Subtype --" %>
</div>
You should have a look at the Group section in the simple_form_for github page. I was working on something similar to what you were doing and found this section. It gave me the direction that I needed to go. Instead of doing f.association which I used on other parts, I ended up using f.input with the :group_select and :group_method
f.input :country_id, collection: #continents, as: :grouped_select, group_method: :countries
from
simple_form_for github pag
Good Morning,
I have two scaffolds person and city.
rails g scaffold person :name, city_id
rails g scaffold city :cityname
and one formular view/people/
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :city_id %><br />
<%= f.text_field :city_id %>
</div>
<div class="field">
<%= f.label :cityname%><br />
<%= f.text_field :cityname%>
</div>
it doesnt go, i have to create an object of city and put the id in the city_id hidden_field and after that after create-button selected all should be saved in the database.
Not too hard or? Who would you make this?
No need to pass city_id.
<%= form_tag url_for(:controller => :your_controller, :action => :some_action, :method => :get do %>
<div class="field">
<%= label_tag :name %><br />
<%= text_field_tag :name %>
</div>
<div class="field">
<%= label_tag :cityname%><br />
<%= text_field_tag :cityname%>
</div>
<% end %>
In controller:
def some_action
city = City.find_or_create_by_cityname(params[:cityname])
person = Person.new(params[:name])
person.city_id = city.id
person.save!
end
I am building a form to work with a non activerecord model. I have two problems..
When I point to keyword/id/edit my form's submit button says 'create keyword', and pressing that submit button takes me to the create method on my keyword controller. It should be taking me to the update action.
I would also like to send an attribute called 'id' to my update action. I'm not sure how to do this. Like I said this isn't an activerecord model.
/views/keywords/_form.html.erb
<%= form_for(#keyword) do |f| %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :keyword %><br />
<%= f.text_field :keyword %>
</div>
<div class="field">
<%= f.label :message1 %><br />
<%= f.text_area :message1 %>
</div>
<div class="field">
<%= f.label :message2 %><br />
<%= f.text_area :message2 %>
</div>
<div class="field">
<%= f.label :start_time %><br />
<%= f.text_area :start_time %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
/controller/keywords_controller.rb >> edit action
def edit
result = Keyword.find(params[:id])
result = result.to_array(:get_response, :return, :data, :item)
result = result.first
#keyword = Keyword.new
#keyword.id = result[:item][0][:value]
#keyword.name = result[:item][1][:value]
#keyword.keyword = result[:item][2][:value]
#keyword.message1 = result[:item][3][:value]
if(result[:item][4][:value] != {:"#xsi:type"=>"xsd:string"})
#keyword.message2 = result[:item][4][:value]
end
#keyword.start_time = result[:item][5][:value]
end
/models/keyword.rb
class Keyword
extend ActiveModel::Naming
include ActiveModel::Conversion
def persisted?
false
end
attr_accessor :id, :name, :keyword, :message1, :message2, :start_time
def self.get_keywords(cid)
#get data from webservice
end
end
config/routes.rb
SchoolBeacon::Application.routes.draw do
devise_for :users
devise_for :admins
resources :keywords
end
Change your #keyword = Keyword.new line in your edit action to #keyword = Keyword.find(params[:id]) or however you find the keyword. If you are using normal RESTful rails routes, that should do it.
Actually, you can just do form_for(#keyword, :url => keyword_path(#result) and change your result to an ivar(result becomes #result)
I have a simple Rails form that allows for its associated parent to be edited. I would like to allow the user to submit this section of the form using :remote => true so that the user can add a new parent, and then select the parent in an updated select menu elsewhere in the form. As you can see in the code, I added a submit button to the parents' part of the form, and it even knows whether to say "Create" or "Update," but when I submit it the entire page is refreshed, the entire form is submitted for validation and so forth. How can I accomplish what I want in Rails?
Here is the code in question:
<%= form_for #sermon, :html => { :multipart => true } do |f| %>
<div class="field">
<%= f.label :title %><br />
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :date %><br />
<%= f.text_field :date %>
</div>
<div class="field">
<%= f.label "Speaker" %><br />
<%= f.select :speaker_id, Speaker.all.collect {|p| [ p.name, p.id ] }, {:include_blank => true} %>
</div>
<% #sermon.build_speaker unless #sermon.speaker %>
<%= f.fields_for :speaker, :remote => true, :html => {:data_type => 'html', :id => 'create_speaker_form'} do |g| %>
<%= g.label :name, "Or, add a new speaker:" %><br />
<%= g.text_field :name %>
<%= g.submit %>
<% end %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
probably your question has been answered here How do you submit a rails 3 form without refreshing the page?
if that doesn't help you can try using preventDefault() attaching it to your submit button.
I have this signup for with devise:
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<p><%= f.label :email %><br />
<%= f.email_field :email %></p>
<p><%= f.label :password %><br />
<%= f.password_field :password %></p>
<p><%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %></p>
<%= f.fields_for :profile, resource.build_profile do |t| %>
<div class ="field">
<%= t.label :type, "Are you an artist or listener?" %><br />
<div>Artist: <%= t.radio_button :type, "Profile::Artist", :id => "artist_button" %></div>
<div>Listener: <%= t.radio_button :type, "Profile::Listener", :id => "listener_button" %></div>
</div>
<% end %>
<p class="before"><%= f.submit "Sign up", :id => "new_user_submit" %></p>
<% end %>
Then I have some JQuery that inserts a field dynamically:
$("#artist_button").click(function() {
if ($('input#user_name').parent().hasClass('field')){
$('input#user_name').parent().remove();
}
$(".before").before('<div class="field"><label for="user_name">Artist or Band/Group Name</label><br><input id="user_name" name="user[name]" size="30" type="text"></div>');
});
However, the type attribute that is nested in the user form and the name attribute that is inserted dynamically are not being saved to the database. The values are being entered as null.
Why is this? How can I fix this?
UPDATE:
The name attribute was not attr_accessible and that was causing it to not be saved to the db. However, the type attribute, which is nested inside the user form, is attr_accessible but is still not being saved to the db.
"type" is a "magic" ActiveRecord column name used for single table inheritance which means that you just got hit by convention-over-configuration. You need to rename your "type" column to something else - I usually use "kind" as a column name.
Try putting in your User Model:
attr_accessible :profiles_attributes
along with the rest of devises attr_accessible(email,remeber_me,etc) , do not delete what you have!
Is type an association? If so, you'd have to add :type_id to attr_accessible, too.
Did you add
accepts_nested_attributes_for :profile
in your User model?