Rails ActiveRecord hash displayed on page - ruby-on-rails-3

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

Related

HTML not rendering through EJS

so basically I have a bunch of HTML strings in a MySQL table and I am trying to display then through EJS.
For instance, I have a string that looks like this is a link with some <code>code</code> next to it. In my code I try to display it in that way.
<%- listOfStrings["myString"] -%>
However, as you probably guessed when reading the title, the string seems to be escaped when displaying on the screen.
What's even weirder to me is that I have two tables with such strings, and it works for the first one, while it doesn't for the second one. One difference though, is that the first one is hardcoded, while the second one can be edited through some tool on my website. Encoding is utf32_unicode_ci for both tables, if that matters.
For debugging purposes I tried to store the aforementioned strings in a js variable and display them in the console: then it seems like <and > characters are all escaped for some reason. Is there an explanation to this behavior, and if so how to fix it so that HTML renders correctly?
Thanks for your help!
You can try it :
<%=listOfStrings["myString"]%>

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 #

simple_form collection checkboxes add unexpected values to database

Environment: Rails 3.2.1
SimpleForm 2.0.1
I have encountered users here and elsewhere asking similar questions via Google, but I have not yet found answers!
I want to provide a series of checkboxes to define a contact's preferences.
I made a small test app using something I noticed on the simple_form demo app.
My Contact model has a string attribute named "post_pref"
In ContactsHelper I included this:
def contact_preference_options
['High Resolution','Web Resolution','Browser','Hard Copy Proof']
end
In my _form partial I include this:
<%= f.input :post_pref, :collection => contact_preference_options, :as => :check_boxes %>
I started the server and created a new Contact.
In the New and Edit views, the checkboxes show up. I checked "Browser" as a preference for my new Contact and submitted it.
The result of checking one or several preferences is a mess.
First:
when I go to edit a record, the current preferences aren't checked.
Second:
In the Show and Index views I see this:
--- - Browser - ''
In the console, I see this:
---\n- Browser\n- ''\n
What I want to see is this:
Browser
My questions are:
Where are the dashes coming from?
Where are the new lines coming from?
Why is there an empty string?
Why aren't the previously selected checkboxes checked when I edit a Contact?
Note: when I did the above with radio buttons or select options it works fine. But I want the contact to potentially have several preferences so I need checkboxes.
Interestingly, I tried the same thing with formtastic instead of simple_form and got almost identical results. What's the trick for checkboxes?
Thanks a million for any help.
For me it looked like it was saving the array, so I did some preprocessing of the parameters before saving it:
lifestyle = params[:lifestyle]
lifestyle[:languages] = lifestyle[:languages].reject(&:blank?).join(",")
if #lifestyle.update_attributes(lifestyle)
...
In this example, my checkboxes was languages under the lifestyle model. Hope this helps.

Best way to hook into a Rails 3 page lifecycle

I have the following scenario: I am adapting a edit in place library to be able to display formatted values after the value is edited on an input field.
For that, I want to store the id for some fields being rendered on a given controller action to be served via AJAX whenever needed.
I want to be able to do the following in a view:
<%= edit_in_place #object, :attribute do |value|
"Formatted display value is #{value}"
end
%>
So, I generate a UUID for the control being rendered:
<input ... data-uuid="27b52850-d68f-012e-5dc8-28373723e63c" ...>
The ultimate goal is to keep a FormattingRules hash that would take the block being passed and assign it to the UUID so I could later on, after the user has edited the value, call an AJAX method to format the value:
http://server/format/27b52850-d68f-012e-5dc8-28373723e63c?value=My+Value
The following code is triggered:
def show
block = BestInPlace::FormattingRules[params[:id]]
render :text => block.call(params[:value])
end
And the cod returns:
Formatted display value is My Value
Which is great. With this, I can hide the edit in place input and display the formatted value.
Everything works already, however the FormattingRules hash is growing indefinitely and never being emptied.
My question is: is there a way I can hook into the Rails lifecycle and make this hash more granular (per page or session, maybe?) and make sure it's gone after the page is no longer being used?

rails3 sort where clause issue

I followed a tutorial on rails3 sorting, not sure which one, and i got it working. I have a table, and depending on the button pushed, I wanted to be able to sort on one of the columns.
Here is an example of the buttons:
<%= link_to "Time", :sort => "transit_time" %>
The column looks like this:
<%= result.transit_time%>
(multiple times obviously)
In the controller, I originally did:
#results = Result.order(params[:sort])
And this worked.
However, now I have added a where clause to be able to query more specifically, so my query looks like this:
#results = Result.where("(NOT(train) OR :traincheckbox)AND . . . :end_location => params[:end_location]}).order(params[:sort])
THIS NO LONGER WORKS. The reason is because when I click the button, it reloads the page, and eliminates all the query string. When I first load the page the query string looks like this:
"http://localhost:3000/results?utf8=%E2%9C%93&start_address=24+Grosvenor+Square&start_l . . .etc."
and once I push one of the sort buttons it turns to this:
http://localhost:3000/results?sort=escore
All my results disappear because there is nothing in the query string, nothing to be passed into the WHERE clause.
ANY IDEA HOW TO KEEP THOSE PARAMS PRESENT?
Sorry that is long, but I'm a relative beginner at rails and I need help.
Thanks!
First of all, I would highly recommend you use the MetaWhere and MetaSearch Gem
One way you can keep the variables present in your next screen is to store them in hidden fields (of a form), then when you click sort, pass those hidden fields over to form your query.
Hope this helps!