I have an ajax request responsible for get some cities of a chosen state. So, I have a form using simple_form and two selects, state and city, as follow:
I don't know how to replace the cities of cities select. That's how I did it:
Controller ajax method
def by_state
cities = City.by_state params["state"]
render :json => { :cities => cities }
end
Form
<%= simple_form_for #store, :html => { :class => 'store-filter' } do |f| %>
<table align="center" cellspacing="10" cellpadding="2">
<tr>
<th colspan="2">Store list</th>
</tr>
<tr>
<td>State:</td>
<td>
<%= f.input :state, :collection => State.all, :include_blank => false, :label => false, :input_html => { :id => "state", :name => "state" } %>
</td>
</tr>
<tr>
<td>City:</td>
<td>
<%= f.input :city, :collection => City.find(:all, :conditions => { :state_id => 1 }), :include_blank => false, :label => false, :input_html => { :id => "city", :name => "city" } %>
</td>
</tr>
.
.
.
Ajax
$("#state").change(function() {
var state_id = $(this).val();
$.ajax({
type: "GET",
url: "cities/state/"+state_id,
success: function(data) {
console.log(data); //returns Object with Array of Cities
$("#city").html(data["cities"]); //replaces to nothing city select
}
});
});
So, when I choose a state, the city select is replaced by null. Why?
Thanks!
I think there is no need of AJAX. You can Populate one dropdownlist from another dropdownlist.
Here is lot of useful information : http://forums.asp.net/t/1431726.aspx
Related
I am working on the Padrino admin screen and would like to show a dropdown list using haml in the edit view. In the List view I would like to show the country name rather than the id number it shows is this possible? I am a very newbie to haml.
I am using countries and states
_form.haml
<code>
- error = #province.errors.include?(:countries_id)
%fieldset.control-group{:class => error ? 'has-error' : ''}
=f.label :countries_id, :class => 'control-label'
.controls
=f.select(:countries_id, :collection => #cntrylist, :fields => [:country_name, :id], :selected => :id, :include_blank => false , :class => :dropdown, :prompt => "Select Province/State")
%span.help-inline=error ? f.error_message_on(:countries_id, :class => 'dropdown-error') : pat(:example)
- error = #province.errors.include?(:province_name)
%fieldset.control-group{:class => error ? 'has-error' : ''}
=f.label :province_name, :class => 'control-label'
.controls
=f.text_field :province_name, :class => 'form-control input-large input-with-feedback'
%span.help-inline=error ? f.error_message_on(:province_name, :class => 'text-error') : pat(:example)
- error = #province.errors.include?(:province_code)
%fieldset.control-group{:class => error ? 'has-error' : ''}
=f.label :province_code, :class => 'control-label'
.controls
=f.text_field :province_code, :class => 'form-control input-large input-with-feedback'
%span.help-inline=error ? f.error_message_on(:province_code, :class => 'text-error') : pat(:example)
.form-actions
=f.submit pat(:save), :class => 'btn btn-primary'
=f.submit pat(:save_and_continue), :class => 'btn btn-info', :name => 'save_and_continue'
=link_to pat(:cancel), url(:provinces, :index), :class => 'btn btn-default'
</code>
This works as expected for new but for edit I would prefer to display the dropdown rather than id which it shows
index.haml
%tbody
-#provinces.each do |province|
%tr.list-row
%td.list-column.list-selectable
=check_box_tag 'province_ids[]', :value => province.id, :class => 'list-selectable-checkbox'
%td.list-column=province.id
%td.list-column=province.countries_id
...
In the td.list column where it shows the countries_id I would like to have this as the country_name.
Thanks
Here is my form view code:
<div class="row">
<%= semantic_form_for #new_athlete_sport, :remote => true, :html => { :class => "new_sport", :"data-type" => 'json', :id => '' } do |f| %>
<%= f.label "Sport" %>
<%= f.select :sport_id, Sport.all.collect { |sp| [sp.name, sp.id] }, {}, { class: "chosen", id: "" } %>
<br />
<%= f.submit %>
<% end %>
</div>
For some reason the <form> tag isn't showing up in the DOM, but every other fields show up..
Seems the syntax in form_for has some problem.
Try remove "id" and move "data-type" from this
<%= semantic_form_for #new_athlete_sport, :remote => true,
:html => { :class => "new_sport", :"data-type" => 'json', :id => '' } do |f| %>
to this
<%= semantic_form_for #new_athlete_sport, :remote => true, :data=> {:type=> 'json'}, :html => { :class => "new_sport"} do |f| %>
I'm new to ROR but I face this problem:
in the index page I have
<%= button_to 'Yes', { :action => 'vote', :id => poll.id, :user_answer => 'yes' }, :method => :post %>
when user presses the "Yes button" the url that passed to the controller contains all the parameters explicitly to the user.
vote?id=1&user_answer=yes
in routes.rb I have:
match 'vote' => 'polls#vote', :via => :post
Any help is appreciated
edit: the entire index.html.erb
Polls
<% #polls.each do |poll| %>
<p>
<%= poll.question %>?
<%= button_to 'Yes', { :action => 'vote', :id => poll.id, :user_answer => 'yes' }, :method => :post %> (<%= poll.yes %>) /
<%= button_to 'No', { :action => 'vote', :id => poll.id, :user_answer => 'no' }, :method => :post %> (<%= poll.no %>)
</p>
<% end %>
The default method for button_to is post, so no need to explicitly specify it unless you want to use other verbs (get, put).
If you don't like the params appended to the url, you might want to use a form instead.
Or, just add the form hash to your button_to tag:
<%= button_to 'Yes', { :action => 'vote', :id => poll.id, :user_answer => 'yes' }, :form => {:data_type => <your choice (html, json)> %>
I am trying to format a form and the text fields respond to some methods, and not others.
I can do things like:
f.input :name, :input_html => { :maxlength => 10 }
f.input :name, :input_html => { :disabled => true }
But if I try to do any of the following, they do not work:
f.input :name, :input_html => { :size => 10 }
f.input :name, :input_html => { :class => 'autogrow' }
f.input :name, :input_html => { :rows => 10, :cols => 10 }
When I try using :size, for instance, the generated html shows that size=10, but is not reflected in the actual form.
These were more or less pulled right from the Formtastic documentation on Github, which the Activeadmin documentation refers to.
I am not sure if your question is solved or not.
However according to Formastic Official WIKI, your code should work:
Customize HTML attributes for any input using the :input_html option.
Typically this is used to disable the input, change the size of a text
field, change the rows in a textarea, or even to add a special class
to an input to attach special behavior like autogrow textareas:
<%= semantic_form_for #post do |f| %>
<%= f.inputs do %>
<%= f.input :title, :input_html => { :size => 10 } %>
<%= f.input :body, :input_html => { :class => 'autogrow', :rows => 10, :cols => 20, :maxlength => 10 } %>
<%= f.input :created_at, :input_html => { :disabled => true } %>
<%= f.input :updated_at, :input_html => { :readonly => true } %>
<% end %>
<%= f.actions %>
<% end %>
https://github.com/justinfrench/formtastic
if your code doesn't work , please check out the error logs, or put more debug info to your erb file, to see if you r rails is running under production mode.
i had the same problem. i wanted a nested form for edit with custom text field size.this worked for me.
form do |f|
f.inputs "Header" do
cf.input :name, :input_html => { :class => 'some_style', :rows => 2, :style => 'width:50%'}
end
f.actions
end
so basically u have to create your own class or just work with the :style.
For nested form u can use this code
form do |f|
f.inputs "Header" do
f.has_many :name,:allow_destroy => true,:new_record => true do |cf|
cf.input :first_name, :input_html => { :class => 'some_style', :rows => 2, :style => 'width:50%'}
end
end
f.actions
end
I have the following form_tag working:
<%= form_tag url_for(:controller => "profiles", :action => "remove_academic", :method => :delete), :id => "remove_major_goal", :remote => true do %>
However, the HTML produced shows that :method => "delete" isn't working. So I found a few answers here on form_tag and tried this:
<%= form_tag url_for({ :controller => "profiles", :action => "remove_academic", :method => "delete" }, { :id => "remove_major_goal", :remote => true }) do %>
However that kicks back an error. What am I doing wrong?
DELETE is not a valid value of the method attribute for a HTML form element. You would probably be better inserting a <input type="hidden" name="method" value="delete" /> inside the form (or use a helper method to do so).
Update:
Try one of these:
form_for url_for(:controller => "", :action => ""), :method => "delete", …
form_for { :controller => "", :action => "" }, { :method => "delete", … }
The second set of braces in the second form maybe unnecessary. Likewise, they might be needed in the first form.