Another quick post, just on how to truncate a string within a view. I used the following in order to ensure that my order_item title is never longer than 100 characters:
Title: 100, :seperator => ' ...') %>
Output:
Title: lentesque porttitor
#More than 100 characters
Title: lentesque porttitor, velit vel ullamcorper pharetra, augue erat pharetra risus, quis bibendum fel…
Good Luck!
Adding a Link Within a Flash Notice/Message – Ruby on Rails
flash[:notice] = "Order created - Click here to pay for it!"
flash[:notice] = "Order created - Click here to pay for it!".html_safe
If you’re using rails 3 make sure you throw in the .html_safe or your link will be appear as plain text. Just a quick screenshot of the end result below:
Anyway, that’s all. Good Luck!
Passing Variables to a Partial from a View – Ruby on Rails
Just another one of those things that I find myself needing to do from time to time. Luckily this is all pretty straight forward (thankyou Ruby on Rails!). Assuming your partial is a form in the locations folder and that the variable you’re expecting is location all that you have to do is following:
View: users/show.html.erb
'locations/form', :locals => { :location => @user.location } %>
Partial: locations/_form.html.erb
Street:
Good luck!
Error 324 (net::ERR_EMPTY_RESPONSE) – Ruby on Rails
Unable to load the webpage because the server sent no data.
Here are some suggestions:
Reload this webpage later.
Error 324 (net::ERR_EMPTY_RESPONSE): The server closed the connection without sending any data.
This wasn’t one I had encountered before and unfortunately Google couldn’t give me anything to cheat off. Luckily, a quick review of the server log revealed the following message just before it closed the connection:
#Redirect to edit location path redirect_to '/locations/edit'
Hopefully this’ll be able to help someone else out there, Good Luck!
Custom Form Submit Button Text – Ruby on Rails
Good Luck!
Validation Error Messages not Displaying – Ruby on Rails
— Usually an error would appear here —
Turns out the problem was pretty straight forward – it’s always the little things! I’d simply missed the each when displaying the error messages:
prohibited this message from being saved:
-
Country can’t be blank
Good Luck!
No HTTP_REFERER was set in the request to this action, so redirect_to :back could not be called successfully
No HTTP_REFERER was set in the request to this action, so redirect_to :back could not be called successfully. If this is a test, make sure to specify request.env[“HTTP_REFERER”].
#Redirect_to_back
def go_back
#Attempt to redirect
redirect_to :back
#Catch exception and redirect to root
rescue ActionController::RedirectBackError
redirect_to root_path
end
#Redirect user to previous page
go_back
#Redirect user to previous page
go_back
#Return to prevent multiple redirects
return
Placing Multiple Button_To on the Same Line – Ruby on Rails
This is a problem that had me confused for an embarrassingly long time. Rails wraps button_to elements within a form and a div. Unfortunately this will take up 100% of the available width. Thankfully the solution is pretty straight forward – simply wrap buttons in a div and float them left like so:
'button'%>
'button' %>
Using the code above you may also find that the divs aren’t filling as expected, simply add a div with a ‘clear:both’ style to the bottom of the wrapper:
'button'%>'button' %>
Hopefully that doesn’t take anywhere near as long for you guys to figure out as it did me, time for a coffee break I think. Good luck!
Routing Error – No Route Matches [Post] “orders/new”
I ran into a fairly common routing error this morning, thankfully these are fairly easy to fix – usually!
No route matches [POST] “/orders/new”
Simply add the following the route to your routes.rb file:
#Orders
controller :orders do
post ‘orders/new’ => ‘orders#new’
end
How to View a Table’s Structure – Sqlite3
SQLite version 3.7.4
Enter “.help” for instructions
Enter SQL statements terminated with a “;”
cid = 0
name = id
type = INTEGER
notnull = 1
dflt_value =
pk = 1
cid = 1
name = user_id
type = integer
notnull = 0
dflt_value =
pk = 0
cid = 3
name = created_at
type = datetime
notnull = 0
dflt_value =
pk = 0