Suppose this form:
<% form_for(#student) do |f| %>
<%= f.select(:subject_id,
options_from_collection_for_select(#subjects, :id, :name),
{:prompt => 'Select a subject' },
{:onChange => "#{remote_function(:update => :student_exam_id,
:url => {
:action => :update_exams_list,
:subject_id => 1
}
)
}" } ) %>
<%= f.select(:exam_id,
options_from_collection_for_select(#exams, :id, :title) ) %>
<% end %>
When user selects a subject, then exams selector list must be updated with exams belongs to selected subject.
How can I send subject parameter to controller? I tried to send parameter using :subject_id => 1, but it does not work.
Please suggest me some ways to make this.
If you need more info, please ask me.
Thank you very much.
Greetings.
Hey, I could answer your question with details. Instead you should only watch this screencast:
http://railscasts.com/episodes/88-dynamic-select-menus
It explanins what you need and with more detailed instructions.
Hope it helps.
I changed my select:
<%= f.select(:subject_id,
options_from_collection_for_select(#subjects, :id, :name),
{:prompt => 'Select a subject' },
{:onChange => remote_function(:update => :student_exam_id,
:url => { :action => :update_exams_list },
:with => "'subject_id=' + this.value") } ) %>
and it works. :D
Related
In a form with form.select I want all options to be selected by default. I have age model, in form which is showing all ages I want to be selected all options by default. My code is following:
f.select(:age, options_for_select(Age.pluck(:age, :id) , :selected => #campaign.ages.pluck(:id)),{},{:multiple=>true , :required=>true})
Try this:
f.select(:age, options_for_select(Age.pluck(:age, :id) , :selected => #campaign.ages.pluck(:id)),{:multiple=>true , :required=>true})
I did this through checks. If a user is created as a new record, then I selected all of them and in case of edit, I will only show only selected. As code following speaks of itself:
<% if #campaign.new_record? %>
<%= f.select(:age, options_for_select(Age.pluck(:age, :id), :selected => Age.pluck(:id) ), {}, {:multiple => true,:required => true ,:class => 'form-control'}) %>
<% else %>
<%= f.select(:age, options_for_select(Age.pluck(:age, :id), :selected => #campaign.ages.pluck(:id) ), {}, {:multiple => true,:required => true ,:class => 'form-control'}) %>
<% end %>
I want to preset a textarea with a value but it isn't working.
<%= f.text_area(:self_summary, :input_html => { "date-pre" => "I will get to this later."}, :class => "textareastyle") %>
<%= f.text_area(:self_summary, :value => "I will get to this later.", :class => "textareastyle") %>
I can't figure out the right code for using a predetermined set of options for a multi-select field. I want to have a list of skills in a drop down that users can select from. Here is the code I am using, it works fine as a single select field, but not as a multi-select:
<%= form_for(#user, :html => { :class => "form-stacked" } ) do |f| %>
...
<div class="clearfix"><%= f.select :skill_list, options_for_select(["Asst", "dir", "pres"]),
{
:multiple => true,
:class => "chzn-select",
:style => "width:450px;" } %></div>
...
<% end %>
Anyone have any suggestions? Eventually, I will want to store all of the options for the multi-select form elsewhere because there will be a bunch, but this is the first challenge I can't figure out..
Thanks.
EDIT
I have also tried:
:html => { :multiple => true, :class => "chzn-select", :style => "width:450px;" } and it doesnt work either
There needs to be two pairs of brackets, one for the options, and one for html_options, like so:
<%= f.select :skills_list, options_for_select(["Asst", "dir", "pres"]), {}, {:multiple => true, :class => "chzn-select", :style => "width:450px;" } %>
See the docs for the select helper.
I'm developing project on rails 2.3.8 and I need to refresh the whole web page when user select particular selection on drop-down menu. How can I do it on rails ?
This is my drop down menu
<%= collection_select("country", "id", #countries , :id, :name, {:prompt => true}, :id => 'xx') %>
<%= observe_field('xx', :url => { :controller => 'calendar', :action => 'update_country' },:update => 'app_header',:with => "'con=' + escape(value)")%>
This finely load the countries so how I can reload the whole page ? please can some one explain me about this?
Simply add a html option to your collection select , onchange => "Javascript code to reload the page"
<%= collection_select("country", "id", #countries , :id, :name, {:prompt => true}, :id => 'xx', :onchange => "location.href = '#{root_url}'") %>
Or you can just refresh the current page:
<%= collection_select("country", "id", #countries , :id, :name, {:prompt => true}, :id => 'xx', :onchange => "location.href = window.location.href") %>
OK, I'm totally new to Ruby on Rails. I created a form that posts to an external widget that returns JSON. So I have this form:
<%= form_for :email, :url => 'http://XXX.XX.XXX.212/widgetapi.0.1.php', :html => {:class => "new_email"} do |f| %>
<%= f.text_field :email, :value => "Your email address...", :class => "text", :id => "email", :name => 'email',
:onFocus => "change(this,'#222222'); this.value=''; this.onfocus=null;",
:size => "26" %>
<%= f.hidden_field :apiKey, :id => "apiKey", :name => 'apiKey', :value => "ABC123" %>
<%= f.hidden_field :lrDomain, :id => "lrDomain", :name => 'lrDomain', :value => "signup.triplingo.com" %>
<%= f.hidden_field :urlPrefix, :id => "refCodeUrl", :name => 'refCodeUrl', :value => "http://signup.website.com/" %>
<%= f.hidden_field :ref_code, :id => 'ref_code', :name => 'ref_code', :value => #referralid %>
<%= submit_tag "Enter To Win", :class => "button-positive submit" %>
<% end %>
Which works. Now I get back a JSON response that is:
({"email":"testing#testing2.com","reflink":"fi1ts","newuser":true})
Ok now the result it the browser sits on the response page with the JSON.
I'm guessing I need to do something with the #response in the controller, but I'm not sure what. All I want to do is if that "newuser" is true, provide them a success page. If false, go to an error page.
Thanks.
You should change that form, so that it will send request to your controller. In that controller you should do API Call with parameters from a form(using for example Curb: https://github.com/taf2/curb or Net/http: http://www.ruby-doc.org/stdlib/libdoc/net/http/rdoc/index.html). Then You can parse result in JSON and show user correct page.
So:
1) User send request to YOUR application
2) YOUR application make request to http://XXX.XX.XXX.212/widgetapi.0.1.php using data from user
3) YOUR application receives JSON, and check if newuser is true.
4) If it is true action render success page, otherwise it render error page.