Rails Random Number Between 0 and 3 always 0 or 1 - ruby-on-rails-3

I am using random numbers to try to call a random partial. I have the erb:
<% #random_partial = 'man_tests/test' + [*0..3].sample %>
<%= render partial: #random_partial %>
But I have also tried rand(0..3).round.to_s and the same thing happens: No matter how many times I click the button, it shows me _test0.html.erb or _test1.html.erb. I have tried enough that it would be statistically crazy if it was by chance that I only ever got these two. I even tried switching the content from _test0 and _test3 to see if it was somehow causing the problem and I still only got the ones named 0 and 1.
Can anyone help me figure out what's causing this problem?

Check this fiddle out:
print 'man_tests/test' + Random.rand(100).to_s
Also, make sure you are not getting served some cached page by your server.

Related

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

uninitialized constant CallsHelper::Active

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.

RnR: Spree changing page title on static pages

So I have a half a dozen static pages such as about us, security, FAQ, etc. How can I change the page title from the default on these pages? I've tried a few tricks from just Rails like helper class, #page.title, etc. and it doesn't seem to work.
I saw this question: https://groups.google.com/forum/?fromgroups#!searchin/spree-user/title$20/spree-user/I6NSK7hG1Kk/i0kfcqiDIUYJ
But that didn't work either, I get undefined method `title=' for nil:NilClass.
Then I found this:
http://guides.spreecommerce.com/release_notes_0_9_0.html
Doesn't work either.
Seems like it should be simple...
Looks like it was simple, the Google Groups solution was:
<% #controller.title = 'The title you want to use on the page you'd like to use it on' %>
However:
<% controller.title = 'The title you want to use on the page you'd like to use it on' %>
works just fine; so it was close just a pesky #

Rails ActiveRecord hash displayed on page

I am creating a sortable list of items from my database using the railscast found here: http://railscasts.com/episodes/228-sortable-table-columns
The sorting works fine, but when the page displays, it seems to print out some sort of hash above the table according to the number items. Each unit looks something like this:
#<ActiveRecord::Relation:0x00000006f9a500>
I've checked the html code and there is nothing in it that prints anything. Is this some sort of debug statement? How do I get it to stop printing?
You are printing object, but you need only one attribute.
Do something like this:
<%= your_object.YOUT_ATTRIBUTE %>