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.
Related
i am new to rails and any help would be much appreciated.
i am trying to sort my list by status, but i have string in code stating "Pending"
i believe the error is prompting up because it can not place the status in order because i have a string
could one kindly advise the best way to go about sorting out a list in alphabetical order where by the list contains a string - thank you
error message
comparison of String with nil failed
jobseekerpg.html.erb
<% #userj_applications.sort_by(&:status).each do |application| %>
<%= link_to application.advert.title, userr_advert_path(application.advert.userr, application.advert) %> |
<%= application.advert.city %> |
<%= application.advert.category_country.name %> |
<span>
status:
<% if application.status == nil %>
<%= "Pending" %>
<% else %>
<%= application.status %>
<% end %>
</span> |
applied: <%= application.created_at.strftime("%B %d, %Y") %><br>
<% end %>
static_controller.rb
class StaticPagesController < ApplicationController
respond_to :html, :xml, :json
def jobseekerpg
#userj = Userj.find(current_userj)
#userj_applications = #userj.forms
end
end
my suggestion:
i currently have a column named status in my table
do i create another column in my table called statusPending
make this as a hidden_field and assign it to pending statusPending:Pending
so whenever an application status is nill i can use an if & else statement to call up application.statusPending
but then i will not need to only sort by status , i will need to sort by status & statusPending
any advise or help will be much appreciated
could one kindly advise the best way to go about sorting out a list in
alphabetical order where by the list contains a string - thank you
Array#sort
stringList.sort!
The "array.sort!" should sort the list in-line.
Here are some ruby examples with blocks http://www.dotnetperls.com/sort-ruby
Looks like you are getting that error when the sort_by method tries to compare a non null status with a null one. You could try to replace your statement:
#userj_applications.sort_by(&:status).each do |application|
with
#userj_applications.sort {|x,y| (x.status || '') <=> (y.status || '') }.each do |application|
This way nil values are treated as empty strings during sort.
I have a link which reads
Collection
and another which reads
Wantlist
They link to the same view but produce different lists.
On that view page I want to be able to alter the text depending on if it is a Collection or Wantlist page. Something like this:
<% if :status == 'Got' %>collection<% elsif :status == 'Want' %>wantlist<% end %>
Obviously that doesn't work but after much experimentation I can't work out how to query which status was passed in the link. Is this possible?
You should be doing it like this
<% if params[:status] == 'Got' %>Collection
<% elsif params[:status] == 'Want' %>Wantlist
<% end %>
The parameters you pass to link_to get put into the params hash. Here, params[:status] is how you'd access it, either in the view or - better - in the controller.
link_to_if might be alternate solution for your question.
link_to_if(params[:status] == 'got', "Collection", user_collections_path(#user)) do
link_to "Wantlist" user_collections_path(#user)
end
Hi maybe this is a fool question, but i really try to look couldnt find any relevant info, (maybe cause i dont know where exactly to start looking at) ..
I need to build a select box with options so the user can "filter" by the candidate.poisition value i have. at this moment the view has this code..
I was thingin on using :params, to post them in URL (i have always do php, im doing rails cause my task now is to insert a design, i achieve everything, just missing a selectbox filter)
Anyway.. here is the code.. wich is pretty simple
<% #candidates.each do |candidate| %>
<% if (candidate.active == true) && (candidate.position.present?) %>
All the html code with info.. like
<%= candidate.name %> : <%= candidate.position? ? t('generales.'+candidate.position) : t('activerecord.models.elite') %>
<% end %>
<% end %>
how can i make this , as im really a noob in Rails, im a regular user on PHP not even really good jaja, pls, i hope someone can give me a hand on this.
Can I use something like this : How to do a LIKE query in Arel and Rails?
Like candidates.where("position like '%?%'", params[:query]).to_sql
Also how can i build the Post as you do in PHP , no idea, to change the position that you want to filter.
Model Candidate.rb just have this inside :S
class Candidate < ActiveRecord::Base
end
Basically you're right. Here is improved code:
<% #candidates.each do |candidate| %>
<% if candidate.active && candidate.position.present? %>
<%= candidate.name %> : <%= t("generales.#{candidate.position}") %>
<% end %>
<% end %>
Update per request in comments:
Use code like this in controller:
if params[:position] == 'red'
#candidates = #candidates.where(position: 'red')
elsif params[:position] == 'blue'
#candidates = #candidates.where(position: 'blue')
else
#candidates = Candidate.all
end
and build html form that will pass parameter position.
or you can use https://github.com/activerecord-hackery/ransack
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
I'm trying to get some code to display based on a condition. I have a boolean field in a table called "show_weekly". What I'm attempting is: If the column == 1 then display the first line else display the 2nd line of code. for some reason it's only showing the 2nd line.
<% if #listing.show_weekly == 1 %>
<%= number_to_currency(#listing.price/4, :unit => "£") %> / week
<% else %>
<%= number_to_currency(#listing.price, :unit => "£") %> / month
<% end %>
any help is greatly appreciated. thanks
The value of a boolean column will either be false or true in ruby, not 0 or 1. So you should do:
<% if #listing.show_weekly %>
instead of <% if #listing.show_weekly == 1 %>
I'd suggest adding a method to your model
def print_price
#p = this.show_weekly? ? this.price / 4 : this.price
number_to_currency (#p, :unit => "£")
end
you need to check if it equals true, not 1
A boolean value is stored as a 1 or a 0, but it is always interpreted as true or false before being returned from the model. This is a source of a lot of confusion.
Using the accessor method show_weekly? can help because a question-mark in a method name usually indicates it will return a boolean value, or something that can be evaluated as boolean.
It will be a lot more readable if you have:
<% if #listing.show_weekly? %>
...
<% else %>
...
<% endif %>
Wherever possible, avoid comparing to specific hard-coded values. For instance, the following is redundant and yet I see it all the time:
# Example: Comparison to nil
if (#something == nil)
# Should be: Checking if initialized
if (#something)
# Example: Comparison to true
if (#something == true)
# Should be: Testing directly
if (#something)
As there are only two values that evaluate as false in Ruby, nil and false, it is generally the case that anything that is not true is nil. Occasionally you will have boolean columns that can be true, false or nil if it is not defined, but this is unusual and can trip up people.
If #listing.show_weekly contains a boolean value, just test for true :
<% if #listing.show_weekly %>
.....
<% else %>
....
<% end %>
Notice you don't even need the "== true". This is because the IF statement only looks to see if there's a true value returned from whatever expression follows it. If #listing.show_weekly contains a value of true then that's all the IF statement sees if that's all you provide it.