I am trying to detect whether a user changes there first name or last name. I was wondering if you can use current_user with dirty attributes? I was thinking along the lines of
<% if current_user.first_name.changed? == true || current_user.last_name.changed? == true %>
<h4>Name Changed</h4>
<% end %>
I get this error
undefined method `changed?' for "john":String
Does anyone know of a way of doing this? might be missing something obvious. Thanks
-John
Not .changed?,it is _changed?
<% if current_user.first_name_changed? == true || current_user.last_name_changed? == true %>
<h4>Name Changed</h4>
<% end %>
See here
Related
I am using a gem for devise called Last_enable https://github.com/ctide/devise_lastseenable and I would like to implement an if statement on my user view , to display a logo "Online" if this statement is true .
My user controller :
def online
online = User.where('last_seen > ?', 5.minutes.ago)
end
the 'online' query works in console and return a user. I just dont really know how to implement that on my view, or do a check if this statement is valid :
<% if #What should I write here to check if my user is online ? %>
<%= image-tag('assets/true.png') %>
<% else %>
<%= image-tag('assets/point_red.png') %>
<% end %>
Thanks in advance for your help !
I just put that #online I missed the # and it worked
I'm setting up my vote system, and trying to have a helper model so I can check if a user has voted for a card. I'm new to rails and can't seem to figure this one out.
How do I have the model check votes for a record that has the user_id of the current_user and the card_id?
I'm also trying to limit calling the helper many times for each iteration of _cards.html.erb by setting the voted variable. Not sure how to do this, trying to set the variable is just printing true for every card, even the ones that have no votes.
Setting the variable is not working and neither is the helper, as it is always true.
cards_controller.rb:
def if_voted(card_id)
if Vote.where(:user_id => current_user.id, :card_id => card_id) then
true
else
false
end
end
helper_method :if_voted
_cards.html.erb:
<td>
<%= #voted = if_voted(card.id) %>
<% if #voted == true %>
<span class="green"><center>
<% elsif #voted == false %>
<span class="red"><center>
<% else %>
<span class="gray"><center>
<% end %>
<%= card.up_votes - card.down_votes %>
</center></span>
</td>
With the help of #tadman
cards_controller.rb
def if_voted(card_id)
if Vote.where(:user_id => current_user.id, :card_id => card_id).any? then
#vote = Vote.find_by(:user_id => current_user.id, :card_id => card_id)
return #vote.voted
else
return nil
end
end
helper_method :if_voted
_cards.html.erb
<td>
<% #voted = if_voted(card.id) %>
<% if #voted == true %>
<span class="green"><center>
<% elsif #voted == false %>
<span class="red"><center>
<% else %>
<span class="gray"><center>
<% end %>
<%= card.up_votes - card.down_votes %>
</center></span>
</td>
Thank you
The where method always returns a scope even if that scope does not contain any records. The find_by method uses the same options but returns either the first matching record or nil if none are found.
That's not quite what you want here, though. You don't actually want to retrieve any of the records, but instead just check if they exist. The any? method on a scope is true if one or more records exist, or false otherwise.
You should update your code to look like this:
def if_voted(card_id)
Vote.where(:user_id => current_user.id, :card_id => card_id).any?
end
It's worth noting a few things about your Ruby style:
Using then at the end of an if clause, while supported, is extraneous and generally not done.
Comparing things == true is usually a sign your logic is confused. If you're concerned about something being literal true rather than just logically true, use === true instead. In this case, close enough counts, so if (if_voted(...)) should suffice.
Your method returned either true or false but you had three conditions as if you were expecting a maybe to pop up one day.
Method names like if_voted are a little clumsy, especially if used inside an if. Something like has_voted? is much more in line with Ruby and Rails in general, so you get if (has_voted?(...)) which reads a lot better.
Even better would be to migrate this method into the User class so you can eliminate the helper and end up with if (current_user.has_voted?(card_id)) as a very clear way of expressing your intent.
I'm trying to figure out at this point how to display only a certain amount of categories based on a user plan. So in my controller I have the following;
def check_plan?
User.current.plan_id == "" && User.current.bins.count == 2 || User.current.plan_id == "nil" && User.current.bins.count == 2
end
helper_method :check_plan?
NOTE: I know, not pretty but it will do for now. So basically :check_plan? checks a few things.
Now I thought, that I could use that for our category list as a starting point, so that if the user isn't on any plan, they are limited in showing 2 if they are on a plan it shows them all. Here's what I thought would work but it doesn't. I've done it before but can't remember exactly how it went, so any help is appreciated.
<% if check_plan? %>
<% #bin_list.order("position").limit(2).each do |bin| %>
<% else %>
<% #bins_list.order("position").each do |bin| %>
<% end %>
Not really working and I know why, but their will all my other tries with bringing any of those lines together with check_plan.
Such logic belongs to Model natually. Don't abuse helper.
class User < ActiveRecord::Base
def bin_limit
check_plan? ? 0 : 2
end
def check_plan?
# Your logic
end
def bins_with_limit
bins.with_limit(bin_limit)
end
end
class Bin < ActiveRecord::Base
def self.with_limit(limit)
return self if limit <=0
self.limit(limit)
end
end
View
current_user.bins_with_limit.each do |bin|
First of all, the rails method .blank? will return true for either "" or nil, so you can start by refactoring your code as follows:
def check_plan?
( User.current.plan_id.nil? || User.current.plan_id.blank? ) && User.current.bins.count == 2
end
helper_method :check_plan?
Secondly, you're calling blocks in the code below, so you'll need to complete them with end
<% if check_plan? %>
<% #bin_list.order("position").limit(2).each do |bin| %>
<% end %>
<% else %>
<% #bins_list.order("position").each do |bin| %>
<% end %>
<% end %>
Of course, you'll want to put whatever you're doing with bin between the do and end portions above.
<% if dashboard_pane_counter.remainder(3) == 0 %>
do something
<% end>
If dasboard_pane_counter wasn't defined, how can I get this to evaluate to false rather than throw an exception?
<% if defined?(:dashboard_pane_counter) && dashboard_pane_counter.remainder(3) == 0 %>
# do_something here, this assumes that dashboard_pane_counter is defined, but not nil
<% end %>
When using rails and instance variables, nil has a try method defined, so you can do:
<% if #dashboard_pane_counter.try(:remainder(3)) == 0 %>
#do something
<% end %>
so if the instance variable is not defined, try(:anything) will return nil and therefore evaluate to false. And nil == 0 is false
local_assigns can be used for that, since this question is from a few years ago, I verified that it exists in previous versions of rails
<% if local_assigns[:dashboard_pane_counter]
&& dashboard_pane_counter.remainder(3) == 0%>
<% end %>
It's in the notes here
http://apidock.com/rails/ActionController/Base/render
Posting this answer for beginner coders like myself. This question can be answered simply using two steps (or one if using &&). It is a longer and less pretty answer but helps new coders to understand what they are doing and uses a very simple technique that is not present in any of the other answers yet. The trick is to use an instance (#) variable, it will not work with a local variable:
if #foo
"bar"
end
If #foo is defined it will be return "bar", otherwise not (with no error). Therefore in two steps:
if #dashboard_pane_counter
if #dashboard_plane_counter.remainder(3) == 0
do something
end
end
Another way, with a neat gem, is 'andand.'
https://github.com/raganwald/andand
Insted of
if !var.nil?
I would use
unless var.nil?
Thats much better ruby code!
in my database I have a field which holds foreign keys. Sometimes the values are NULL or 0.
I know the helper blank?. Is there something similar to enable if there is a number set in the field? Because blank doesn't work here.
the code for the view is something like this
<%= #b.author unless #b.author_id.blank? %>
you could write your own helper
def identified? author
author.id.blank? or author.id == 0
end
You could try something like:
<% if #b.author_id == 0 %>
#display something here
<% else %>
#display something else
<% end %>
in your view.