I tried doing this when in rails console:
puts about_home_index_path
It gave me an erorr, why is that? should it work?
It was answered here: Rails: how do you access RESTful helpers?
You should use:
puts app.about_home_index_path
Related
I want to inspect my array with all relation (belognTo and hasMany) in ruby and rail without loop in controller file. is there any way such like a php? I have try this
abort(#microposts.inspect)
but error is coming. Who can do this ?
just use
logger.debug "microposts---------->#{#microposts.inspect}"
The above line will print the micropost object in your console log
I suggest that you check Ruby on Rails Debugging Documentation.
I have migrated from Rails3 to Rails4. The following code returns Array in Rails3 but in Rails4 it returns string with illegal character.
Dir.glob("app/assets/images/flowers/*")
sample output in Rails3
["app/assets/images/flowers/rose.png", "app/assets/images/flowers/lilly.png"]
output in Rails4
"\x04\b[dI\"8app/assets/images/flowers/rose.png\x06:\x06ETI\"4app/assets/images/flowers/lilly.png"
How to get same output format as in Rails3?
try this
files = Dir.glob("app/assets/images/flowers/*").map do |f| File.basename f end
Dir has nothing to do with Rails — it's pure Ruby class. Here is the API reference to it. According to API it should always return an Array. My guess is that you messed up something in your Ruby installation while you were upgrading Rails 3 to 4.
I think best bet will be a clean installation of ruby/rails. You could also try to run Dir.glob() from both IRB and rails console to see where the mistake happens; and start from there.
I've been following rails casts #221, i author using find_by_somefield! with "!". What does that mean in context of ActiveRecord find condition?
It raise an ActiveRecord::RecordNotFound error.
reference: rails guide
see the below link. You will get the answer.
http://rorguide.blogspot.in/2011/06/active-record-finder-methods-dynamic.html
[update: by not using rake routes, just to understand Rails console a little more]
It seems like inside of "rails console" for Rails 3, we can use controller, but in Rails 2.2 or 2.3, we need to use #controller
And in Rails 3, we can print out all the routes added by Rails routing for a scaffold foo:
ruby-1.9.2-p0 > puts controller.public_methods.grep(/path|url/).grep(/foo/).sort.join("\n")
edit_foo_path
edit_foo_url
foo_path
foo_url
foos_path
foos_url
new_foo_path
new_foo_url
but on Rails 2.3.8, it gives a bunch of formatted_foos_path, etc, and gives nothing for Rails 2.2.2. How to make it print out for Rails 2.3.8 and 2.2.2?
Details for Rails 2.3.8:
ruby-1.8.7-p302 > puts #controller.public_methods.grep(/path|url/).grep(/foo/).sort.join("\n")
formatted_edit_foo_path
formatted_edit_foo_url
formatted_foo_path
formatted_foo_url
formatted_foos_path
formatted_foos_url
formatted_new_foo_path
formatted_new_foo_url
Rails 3.x–6.x
Rails.application.routes.named_routes.helper_names
Rails 2.x
helpers = Rails.application.routes.named_routes.helpers
This will get you all the named route methods that were created. Then you can do helpers.map(&:to_s), and whatever regex you want to get your foo versions
or load up localhost_path/rails/info/routes in your browser.
Well in Rails 4, I use rake routes. Is it that you need?
Rails >= 6.1
I just upgraded an app to 6.1.4 and noticed that rake routes no longer works.
Instead we can run bin/rails routes
To get a complete list of the available routes in your application, visit http://localhost:3000/rails/info/routes in your browser while your server is running in the development environment. You can also execute the bin/rails routes command in your terminal to produce the same output.
Docs: https://guides.rubyonrails.org/routing.html#listing-existing-routes
I've been doing some development with rails 3 and I was wondering why calling the "puts" method doesn't output to standard out.
How do I get this back?
Instead of puts, use logger.info:
logger.info #collection.inspect
You can also use the standard ruby way by calling STDOUT << 'your output'. Method put is not a rails speciality, it comes with ruby. If you use rails, you can also rely on the logger object.