How to Accept ONLY Two Keys Before User :Submit? - ruby

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']])) %>

Related

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...
}
});

add form (non-nested) dynamically in rails

pls can someone explain how I can dynamically add another copy of a form in rails?...been working on this for almost 2hrs. I messed around with .clone() and .appendTo in jquery, but it didnt work. Also, most of the materials I found online (like on railscast #196 and stackoverflow) focused heavily on nested forms. My form is nested, but I actually just want to add the parent form again. The photos which are nested use the html multiple attributes so I'm guessing that will handle multiple files upload for each parent form (btw I'm using paperclip).
If I just need to modify the railscast code please let me know.
Thanks.
<%= form_for(#user_book, html: { multipart: true }) do |f| %>
<% if #user_book.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#user_book.errors.count, "error") %> prohibited this user_book from been saved:</h2>
<ul>
<% #user_book.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<p>hello</p>
<% end %>
</ul>
</div>
<% end %>
<%= f.text_field :title, placeholder: "enter title...", id: "book_title" %>
<%= f.text_field :category, placeholder: "enter category..." %>
<%= file_field_tag 'user_book[user_book_photos_attributes][][photo]', :multiple => true do |p| %>
<%= p.file_field :photo %>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>

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.

Facebooker2 returning users UID but not name

I am just using the standard Facebooker2 setup.
<%= fb_connect_async_js %>
<% if current_facebook_user %>
<%= "Hello #{fb_name(current_facebook_user, :useyou => false)}!" %>
<%= fb_logout_link("Logout", request.url) %><br />
<% else %>
<%= fb_login_and_redirect('/login', :scope => 'user_about_me') %>
<% end %>
What displays on the page is:
Hello <fb:name uid="73648576" useyou="false"></fb:name>
I am sure there's something very basic wrong here, but I've followed the instructions, done tutorials, wetc. and continue to get this result.
Any help most appreciated!
Not sure why fb_name isn't working, but you could use the other method as specified in the readme. In this case, you need to call the fetch method on current_facebook_user first:
<% if current_facebook_user %>
<% current_facebook_user.fetch %>
<%= "Welcome #{current_facebook_user.first_name} #{current_facebook_user.last_name}!" %>
Ref: http://www.engineyard.com/video/21045039 (around 9:36)

Bizarre behaviour in a nested form - need <%= not <%

I'm using rails 3.1.3
I was trying to make a nested form - I made it work in the end, so I've got all the accepts_nested_attributes_for bits right.
This is the form that worked:
<%= form_for(#article, :as => :article) do |f| %>
......some article fields in here
<%= f.fields_for :article_site_permissions do |builder| %>
<%= builder.label :name %><br />
<%= builder.text_field :name %>
<% end %>
<div class="actions">
<%= f.submit 'Update' %>
</div>
<% end %>
The weird bit is that the line <%= f.fields_for :article_site_permissions do |builder| %> needed <%= not <%. All the examples I've seen (eg http://railscasts.com/episodes/196-nested-model-form-part-1?view=asciicast) can use <% not <%=. Coincidently, later that day the same thing happened on a colleagues project, which prompted this question.
Does anyone know what's going on here?
From rails 3.0 onwards <%= is the right thing to do (see the release notes). The railscast you link to predates rails 3.0.

Resources