uninitialized constant CallsHelper::Active - ruby-on-rails-3

I'm writing a helper in my Calls view that changes the text of a span depending on a evaluation of a date. When I write the if/else in the view I can make it work but I get the error "uninitialized constant CallsHelper::Active" when trying to access it from my helper.
view code:
<%= status_indicator(call)%>
helper code:
def status_indicator(call)
if call.transfer_date > Time.zone.now
Scheduled
else
Active
end
end
I'm not sure what the error is trying to tell me besides it's not working. Can someone give me a hand and let me know where I'm going wrong?

I want to return strings and not Ruby classes. Putting "" around the strings fixed the problem.

Related

PostgreSQL & DataMapper issues

Been scratching my head for a while on this one and despite trying many variations I cannot see the mistake. After writing the app file, which contains what looks like the correct DataMapper.setup code for using PostgreSQL (?), and upon trying to play around in IRB/PRY, i just get a 'FATAL database not created' message even after i have called 'Song.auto_migrate!', here is my code, can anyone help me get past this? Thanks in advance:
require 'data_mapper'
require 'dm-core' #main DataMapper gem
require 'dm-migrations' #extra DataMapper functionality extension
DataMapper.setup(:default, "postgres://localhost/development")
class Song
include DataMapper::Resource
property :id, Serial
property :title, String
property :lyrics, Text
property :length, Integer
property :released_on, Date
end
DataMapper.finalize
I require the file all fine in irb, then call Song.auto_migrate! and it runs the 'database does not exist' error. What am i doing wrong?
You need to do this in command line:
psql
and then
CREATE DATABASE development;
before even trying to run Data Mapper setup code.
Perhaps you are missing this line: DataMapper.auto_upgrade!
On a side note, remember that auto_migrate! will wipe any existing data, whereas auto_upgrade! doesn't: http://datamapper.org/getting-started.html

Rails Displaying Flash Messages from Model

I'm trying to display some error messages from my model in Rails. Currently, when an exception is caught in my test model and raised, the messages are returned as expected in the errors scope of my object. The messages are returned as follows:
{:key1=>["Your key needs to be different."],
:key2=>["Another exception"]}
This is perfect, as the correct exceptions are being thrown for the correct errors. What I'm trying to do now is bind these messages to the flash scope so that I can display them on my view. In my controller, when I have an exception, I then bind it to the flash[:error] scope as follows:
flash[:error] = #test.errors.messages
render :new
Upon doing this, my errors are bound to the flash scope, but when I output them on my view, they are displayed as follows:
[:error, {:key1=>["Your key needs to be different."], :key2=>["Another exception"]}]
All I'd like to do is display each one of these messages so that they look to be like:
Your key needs to be different.
Another exception.
Notice that in the above example, all that will be rendered is the message text.
Will you please point me in the right direction as to what I need to change either in my controller or in my view to achieve the desired output above?
Thank you in advance.
The one place I immediately see for improvement is in the hash you're passing.
If there's not a specific reason you're wrapping your strings in an array, you can eliminate the array:
{
:key1 => "Somebody poisoned the watering hole!",
:key2 => "There's a snake in my boot."
}
That will both simplify your code and eliminate the two-dimensional array you didn't seem to be intending to create.
If there is a reason you're wrapping your strings in an array (multiple messages per key?), you'll need to revisit your iterator; the way it's written now, it's only going to display the first string for any given key.
I noticed that when I looped over #test.errors.messages in the following construct, I ended up with a 2-Dimensional array:
[:key1, ["Your key needs to be different."]]
[:key2, ["Another exception"]]
Since this is a multidimensional array, I was able to then simply loop over the array in my erb and get the value to display as requested in my question:
<% for i in flash[:error] %>
<%= i[1].first %><br />
<% end %>
Returns on the screen:
Your key needs to be different.
Another exception.
I'm always open to suggestions, so if there's a better way to handle this, then please advise! Otherwise, this does work for me.

rails user input with <script>, stored, and displayed

I have an application that collect user input and store to DB and show back to user.
One user entered "alert(1)" into the name field and saved it into DB.
Whenever the name is displayed, the page will be broken.
I know how to fix that input only with validation for input, and h() for output.
However, I have so many input fields and so many outputs that accept users' text.
Is there any simple way to prevent this happening(i.e. overriding params method, etc)?
I also want to know how you expert guys are dealing with this problem?
As of Rails 3, my understanding was that embedded ruby code was html escaped by default. You don't need to use h() to make it that way. That is, if you use <%= "<script>a=1/0;</script>" %> in a view, the string is going to be made html safe, and so the script doesn't execute. You would have to specifically use raw() or something similar to avoid it - which you should naturally not do unless you're really confident about the contents.
For output, Rails 3 automatically html-encode all text unless I use raw() method.
For input, How about making a common validator and apply to all fields that are text or string? Is it desirable?
http://api.rubyonrails.org/classes/ActiveModel/Validator.html
class MyValidator < ActiveModel::Validator
def validate(record)
record.class.columns.each do |c|
if c.type==:text || c.type == :string
record.errors.add c.type, "script tag is not allowed" if c[/<script[^>]*>/]
end
end
end
end

Rails 3 console and db:console are different

I have a rails app and when I have both the console and db:console open, they have different values for what is supposed to be the same field. I am using the send function to change the value. Here is the method in the model:
def toggle_approve(field)
self.send(field)
if(self.send(field).blank?)
self.send(field + '=', "new_value")
puts self.send(field)
else
self.send(req + '=', "")
end
rank.save
end
In my db:console (sqllite) everything is always correct, but in my regular erb console it is wrong. My view will then show what is in the erb console and not the sqllite. I dont understand what is going on in the background that would causing this issue. Any help would be great.
It's hard to be sure from the details you provided, but I think it is the case that your view uses an obsolete version of the data. Try calling .reload on the updated record, just at the point after the update and before presentation. See if that fixes your problem.

How do I send multiple params in my rails3 form controller

I'm trying to send a number of virtual attributes from my form but keep running in to an error about not being able to convert symbol to integer.
My controller's got this in it:
#user = User.generate_batch(params[:user][:username_l][:quantity])
And in my model:
def self.generate_batch(username_l, quantity)
What am I doing wrong and where can I read up on this??
S
For one thing, generate_batch has 2 arguments and you provide only one, and in the wrong place I believe.
For future reference, when passing multiple params, I needed to do the following. I hope it helps someone else.
#user = User.generate_batch(params[:user][:username_l],params[:user][:quantity])