How to create dropdown list values depending on previous input? - ruby

I need some help on rails forms. I currently have:
<div class="field">
<%= f.label :student_id %>
<%= f.collection_select(:student_id, Student.active.order('first_name'), :id, :proper_name, :prompt => true) %>
</div>
<br />
<div class="field">
<%= f.label :section_id %>
<%= f.select(:section_id, get_section_list, :prompt => "Select Section") %>
</div>
So the first drop down is to select a student. The second drop down list is to select a section. However, right now I am displaying all sections. But I want to only the select those sections that the student is eligible for. So how do I get the input of the first drop down list in order to change the second drop down list options. If they try to choose the section without selecting a student, it should show all the options.
Thanks!

Put the select box in a partial and re-render the partial when the first select box changes.
http://keithschacht.com/getting-ajax-to-work-in-rails-3-with-jquery/

Related

ckeditor doesnt show anything in the edit form and the dbms doesnt even load in the editor

I tried the ckeditor and it works fine when adding or making a new post but the problem is when I try and edit the post.
it doesn't load properly and it doesnt show anything like the code , or even a spec, it just shows the pre default ckeditor list and it doesn't even show any data coming from the database.
any help would really be great, and info would be awesome.
I have made it into a form and then rendering it so that I can just call the function like so.
This is the _form
<p>
<%= f.label :text %><br>
<%= f.cktext_area :text, :value => 'Default value', :id => 'sometext' %>
</p>
Now this is in my edit
<%= render 'form' %>
I tried adding the raw into it like so
<%= raw render 'form' %>
or even into the _form but it doesn't seem to show properly
after tinkering and trying the usual thing the codes show it.
<p>
<%= f.label :text %><br>
<%= f.text_field :text %>
</p>
when I try it and add this now it shows here , but the ckeditor doesn't show properly.
Just give this:
<p>
<%= f.label :text %><br>
<%= f.cktext_area :text %>
</p>
I hope you are calling this partial inside a form_for.
Update the answer,
Just remove
:value => 'Default value',

How to Accept ONLY Two Keys Before User :Submit?

I want people to be able to challenge their habits. If they miss a day they may put "/" if they miss two days in a row they must put "X". Three X's means they failed the challenge.
How can I force the User to only be able to submit X's and /'s as valid keys in the :missed input field?
_form excerpt
<%= simple_form_for(#habit) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :missed %>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
Github: https://github.com/RallyWithGalli/ruletoday
Thanks in advance for your help. You rock!
You can use a select tag to limit users' options like this
<%= select_tag(:missed , options_for_select([['/', '/'], ['X', 'X']])) %>

rails 3, 2 check_boxes shouldn't be false at the same time, how?

I have a form like this:
<div class="row">
<div class="span6 offset3">
<%= form_for #user do |f| %>
<%= render '/shared/error_messages' %>
<%= f.label :name %>
<%= f.text_field :name %>
........
<% if current_user.admin? %>
<%= f.label :admin %>
<%= f.check_box :admin, {checked: true} %>
<br /><br />
<%= f.label :developer %>
<%= f.check_box :developer %>
<% end %>
........
<%= f.submit "Invite new user", class: "btn btn-large btn-primary" %>
<% end %>
</div>
</div>
As you see, there are two check_boxs to determine whether the new user admin or developer. Admin is checked by default.
What I want to do is, when somebody click checked admin, it won't be unchecked. When click developer, admin will be unchecked and developer will be checked.
New user must be either admin or developer. Both check_boxes should never false at the same time
This functionality (only one possible choice) is for radio button, not checkbox
use radio buttons. If you still insist on using checkboxes, give both the check boxes a class name, bind the class in a javascript with live and do the validation !
$('.checkboxes').live('change', function(){
if($(this).is(':checked')){
dosomething...
}
});

RoR: How can I get my microposts to show up?

Here is the users show view where they are supposed to show up. ..
<section>
<div id= "purchases">
<%= render 'shared/micropost_form_purchase' %>
</div>
<div id="sales">
<%= render 'shared/micropost_form_sale' %>
</div>
</section>
<%= #sales %> <%# This is just to see if it outputs anything. It doesn't :( %>
<div id="purchases list">
<ol class="microposts">
<%= render #purchases unless #purchases.nil? %>
</ol>
</div>
<div id="sales list">
<ol class="microposts">
<%= render #sales unless #sales.nil? %>
</ol>
</div>
so the forms (partials) are loading fine, but then when I make a post, in either one, neither the purchases list nor the sales list shows up. I checked the database and they are being created along with an entry in the column indicating kind (either sale or purchase).
Here are the forms:
<%= form_for (#micropost) do |f| %>
<div class="field no-indent">
<%= f.text_area :content, placeholder: "What's something else you want to buy?" %>
<%= hidden_field_tag 'micropost[kind]', "purchase" %>
</div>
<%= f.submit "Post", class: "btn btn-large btn-primary" %>
<% end %>
and
<%= form_for (#micropost) do |f| %>
<div class="field no-indent">
<%= f.text_area :content, placeholder: "What's something else you want to buy?" %>
<%= hidden_field_tag 'micropost[kind]', "sale" %>
</div>
<%= f.submit "Post", class: "btn btn-large btn-primary" %>
<% end %>
also, here is the show part of the users_controller.rb
def show
#user = User.find(params[:id])
#micropost=Micropost.new
#microposts = #user.microposts.paginate(page: params[:page])
end
and here is the show part of the microposts_controller.rb
def show
#micropost = Micropost.find(params[:id])
#microposts = Micropost.where(:user_id => #user.id)
#purchases= #microposts.collect{ |m| m if m.kind == "purchase"}.compact
#sales = #microposts.collect{ |m| m if m.kind == "sale"}.compact
end
additionally, with the help of this post (http://stackoverflow.com/questions/12505845/ruby-error-wrong-number-of-arguments-0-for-1#12505865) the variables #microposts, #purchases, and #sales are all outputting correctly in the console.
can anyone help me out?
edit: using scopes as suggested by the answer given works in the console (it outputs everything correctly, but they still don't show up in the view. Does this mean it is something wrong with my syntax for the users show page?
edit 2:
Here is the view/microposts/_micropost.html.erb code
<li>
<span class="content"><%= micropost.content %></span>
<span class="timestamp">
Posted <%= time_ago_in_words(micropost.created_at) %> ago.
</span>
<% if current_user?(micropost.user) %>
<%= link_to "delete", micropost, method: :delete,
confirm: "You sure?",
title: micropost.content %>
<% end %>
</li>
I'm making some assumptions without seeing more of your code, but it looks like you could
write what you've shown a little differently. I'm assuming your databases are migrating
and have the required columns, e.g., Micropost#kind, Micropost#user_id, etc.
You can use scopes to refine a collection of microposts more expressively. It might be helpful to read
up about ActiveRecord scopes: http://guides.rubyonrails.org/active_record_querying.html#scopes.
class Micropost < ActiveRecord::Base
belongs_to :user
scope :purchases, where(:kind => "purchase")
scope :sales, where(:kind => "sale")
# your code
end
I'm also assuming your user has many microposts:
class User < ActiveRecord::Base
has_many :microposts
# your code
end
For your forms, I'd suggest attaching your hidden field to the form object (f.hidden_field) so
you don't have to specify the name as 'micropost[kind]'.
<%= form_for(#micropost) do |f| %>
<div class="field no-indent">
<%= f.text_area :content, placeholder: "What's something else you want to buy?" %>
<%= f.hidden_field :kind, :value => "sale" %>
</div>
<%= f.submit "Post", class: "btn btn-large btn-primary" %>
<% end %>
In MicropostsController#show, you can use your new scopes:
def show
#micropost = Micropost.find(params[:id])
#microposts = #user.microposts
#purchases = #microposts.purchases
#sales = #microposts.sales
end
You should also confirm that your MicropostsController#create action is actually adding
the microposts to the user sending the form (I'm assuming a current user method).
def create
#micropost = current_user.microposts.create(params[:micropost])
# yada
end
You can also confirm expected results on rails console after creating purchases or sales micropost with:
Micropost.purchases
Micropost.sales
Again, I could be missing something without seeing more of the code base.
Check Micropost.count, #purchases.count, #sales.count (by printing them in the controller, or some part of the view) to see if the records actually exist.
Also, if you want to render collections likes #sales and #purchases, you need to make sure that the model partial exists (_micropost.html.erb in your case). That is probably where you need to look for the view errors. For all you know, that file could be empty, thus no errors will show up at all.
The problem might also lie in your microposts#create (or whichever action that you are saving the micropost in), the micropost should be associated with the current_user:
#micropost = current_user.microposts.build(params[:micropost])
Taking this and your previous question into account, I suggest you go through the original code for the RoR tutorial again (and verify that all tests are passing) before taking it apart. You can always add new tests to it for your experiments and they will help in figuring out where you went wrong.

ruby - text parse issues when populating from database

tried to create simple dropdown in ruby with values from the database - like that :
<% ingredientArray = Ingredient.all.map { |ingredient| [ingredient.name, ingredient.id] } %>
<div class="field">
<%= select_tag(:ingredient_id, ingredientArray) %><br/>
</div>
and I received an empy one.
this is the generated html
<div class="field">
<select id="ingredient_id" name="ingredient_id">[["Paprika", 5], ["Cinnamon", 8], ["Salt", 9], ["Pepper", 10], ["water", 11]]</select><br/>
</div>
where should I put html sage
You should read documentation on select_tag and related methods.
The second parameter of it is a string containing the option tags for the select box.
You can generate in manually:
select_tag "people", "<option>David</option>".html_safe
Or use options_from_collection_for_select method for it:
select_tag "people", options_from_collection_for_select(#people, "id", "name")
(Examples from the docs)
Specifically in your case the best way to make that dropdown is:
<div class="field">
<%= select_tag("Ingredients", options_from_collection_for_select(Ingredient.all, 'id', 'name')) %>
</div>
You can also use collection_select like so:
<%= collection_select :recipe, :ingredient_id, Ingredient.all, :id, :name, { prompt: "– Select an Ingredient –".html_safe } %>
(I've assumed the parent object you are trying to assign the ingredient ids to is :recipe. Change that value as it fits your app.)

Resources