First radio_button not showing in Rails 5 - enums

I'm using Devise within Rails 5 and have added Enum/roles (Consumer, Designer) to the User model. When creating a new registration, I want the user to select a role, for which I've added a radio_button for each value/role:
<div id="user_role"
<% User.roles.keys.each do |role| %>
<%= f.radio_button :role, role %>
<%= f.label role.to_sym %>
<% end %>
</div>
It's showing the labels of both roles, but only the radio_button for the last role. Here are the roles defined in the User model:
enum role: [:consumer, :designer]
Any ideas on what I'm doing wrong here?

try to this way
<div id="user_role">
<% User.roles.keys.each do |role| %>
<%= f.radio_button :role, role %>
<%= f.label role.to_sym %>
<% end %>
</div>
or
<div id="user_role">
<% User.roles.each do |role| %>
<%= f.radio_button :role, role %>
<%= f.label role[0] %>
<% end %>
</div>
it's working fine in my local.I hope it's will be help you.

Related

cached votes not updating with validates_presence_of in model

I have acts_as_taggable installed and working for my products with cached votes.
After I tried to finish my validations inside the Product model with validates_presence_of the cached votes are not getting updated anymore.
Anyone had the same problem?
Any hint is appreciated.
Basic validation inside the product model. If this line is not there, the cached_votes work:
validates_presence_of :original_url, :format => URI::regexp(%w(http https))
How I trigger the votes:
<% if user_signed_in? %>
<% if current_user.liked? likeable %>
<%= form_tag unlike_path(likeable_type: likeable.class.to_s, likeable_id: likeable.id), method: :post, remote: true do %>
<% button_tag class: 'btn btn-block liked' do %>
<%= fa_icon 'heart-o' %> unlike
<% end %>
<% end %>
<% else %>
<%= form_tag like_path(likeable_type: likeable.class.to_s, likeable_id: likeable.id), remote: true do %>
<% button_tag class: 'btn btn-block' do %>
<%= fa_icon 'heart' %> like
<% end %>
<% end %>
<% end %>
<% else %>
<%= link_to new_user_registration_path do %>
<% button_tag class: 'btn btn-block' do %>
<%= fa_icon 'heart' %> like
<% end %>
<% end %>
<% end %>

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

my final step in the rails app, how to make comments appear in a popup?

This is the last remaining item to complete my first rails app and need some help.
On each user profile (localhost:3000/users/username), there's a listing of posts that the user has made. Associated with each post are comments. So post_id: 3 could have comments.
I have it working already in view form but I need the comments to appear in a popup instead when the "Comments" link under each post is clicked.
I have already applied facebox which is a jQuery-based lightbox that displays popups.
I just need to move what's currently shown in show.html.erb into a popup.
There's the _comment_form.html.erb which renders into _post.html.erb
<%= link_to #, :rel => "facebox-#{post.id}" do %>
+<%= post.comments.count.to_s %>
<% end %>
<div class ="ItemComments"><% if post.comments.exists? %>
<% post.comments.each do |comment| %>
<%= image_tag("http://www.gravatar.com/avatar.php?gravatar_id=#{Digest::MD5::hexdigest(comment.user.email)}" %>
<span class="users"><%= link_to comment.user.name, comment.user %></span>
<span class="timestamp"><%= time_ago_in_words(comment.created_at) %> ago</span>
<span class="content2"><%= comment.comment_content %></span>
<% end %>
<% end %></div>
The above renders into _post.html.erb using:
<%= render 'shared/comment_form', post: post if signed_in?%>
Then it renders into show.html.erb
I'm trying to use this line, but what do I link it to?
<%= link_to #, :rel => "facebox-#{post.id}" do %>
+<%= post.comments.count.to_s %>
<% end %>
This is shared/_comment.html.erb
<% if post.comments.exists? %>
<% post.comments.each do |comment| %>
<%= image_tag("http://www.gravatar.com/avatar.php?gravatar") %>
<%= link_to comment.user.name, comment.user %>
<span class="timestamp"><%= time_ago_in_words(comment.created_at) %> ago</span>
<span class="content2"><%= comment.comment_content %></span>
<% end %>
<% end %>
One way of doing this is to render your comments into a hidden div and give that div an id. Next you point your link to the id of the div using # followed by the id. It would look something like this:
_post.html.erb
<%= link_to "#comments", :rel => "facebox" do %>
<%= post.comments.count.to_s %>
<% end %>
<div id="comments">
<%= render 'shared/comment_form', post: post if signed_in?%>
</div>
CSS
#comments {
display: none;
}
See the 'Divs' heading over at the Facebox docs.

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 %>

Rails 3 Render / Partial

I'm very new to Rails 3 and I've followed some tutorials and now I'm trying to "play" with the code created. I have followed the tutorial from http://guides.rubyonrails.org/getting_started.html
I'm am trying to render the form for new posts on the homepage with this code:
<%= render :partial => "posts/form" %>
The posts/_form.html.erb looks like this:
<%= form_for(#post) do |f| %>
<% if #post.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#post.errors.count, "error") %> prohibited this post from being saved:</h2>
<ul>
<% #post.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :title %><br />
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :content %><br />
<%= f.text_area :content %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
and this is the error I get:
undefined method `model_name' for NilClass:Class
Extracted source (around line #1):
1: <%= form_for(#post) do |f| %>
2: <% if #post.errors.any? %>
3: <div id="error_explanation">
4: <h2><%= pluralize(#post.errors.count, "error") %> prohibited this post from being saved:</h2>
Trace of template inclusion: app/views/home/index.html.erb
Rails.root: d:/server/cazare
Application Trace | Framework Trace | Full Trace
app/views/posts/_form.html.erb:1:in `_app_views_posts__form_html_erb___794893824_70478136_519766'
app/views/home/index.html.erb:5:in `_app_views_home_index_html_erb__967672939_70487520_0'
I understand that this may seem a piece of cake for some of you but I'm trying to understand how everything works on Rails so I hope you can understand me.
Thanks in advance !
#post variable is not instantiated in the Controller :)
so "#post = Post.new" inside the controller action should do the trick
Rails is attempting to build a form for the object #post. In order to do that, it needs to know what sort of object #post is; that way, it can find any existing data in the object and fill it into the form for you. Rails has a method grafted on to objects called model_name to do the lookup, but it won't be grafted onto NilClass (the class of the nil object).
I suspect that you haven't defined #post anywhere - it's an instance variable of the Controller, so you'd expect the controller to either find #post from the database, or to call #post = Post.new - so it's nil.
In the posts/_form.html.erb,
change
<%= form_for(#post) do |f| %>
to
<%= form_for(Post.new) do |f| %>

Resources