Rails: how do you access RESTful helpers? - ruby-on-rails

I'm trying to work through this guide to Rails routing, but I got stuck in section 3.3:
Creating a RESTful route will also make available a pile of helpers within your application
and then they list some helpers like photos_url, photos_path, etc.
My questions:
Where can I find the complete list of helpers that is "made available?"
Is there a way to call the helpers in the console? I created an app, then opened up the console with script/console. I tried to call one of the helpers on the console like this:
>> entries_url
But got:
NameError: undefined local variable or method `entries_url' for #<Object:0x349a4>
from (irb):8

You have several questions in there, most of which have already been answered by people below.
The answer to one that wasn't fully addressed however, is: yes you can use the script/console to see where your routes go. Just type in app.[route_helper] and it will respond with the path. For example app.users_path will return /users/
So for your example type app.entries_url for the full URL - or app.entries_path for its relative path within the console.

rake routes at the command line should get you that list.

I think this may be what you are looking for ... http://topfunky.com/clients/peepcode/REST-cheatsheet.pdf

You can access other helpers in the console by prepending "helper."; ie. helper.progress_box (assuming #progress_box exists of course)

From memory, you can't call url/path helpers from the console for some reason.

Related

Query which controller will be used for a URL

Question
I'm looking for something in the rails console or similar where I can call get_controller_for_path('/some/path/') and it will return the corresponding controller.
Background
I have a rails project with a lot of routes. I'm investigating a routing problem and want to confirm that a given URL will match a specific route.
I can use bundle exec rake routes to view the list of routes, but that still requires my human eyes to parse the hundreds (thousands?) of routes and figure out what's going on.
Here you go: Rails parse url to hash(Routes)
That says it's Rails.application.routes.recognize_path "/accounts/1"
You can also use assert_routing in your tests. If you don't have tests, now is a very good time to add some. And you can read the source of assert_routing, and assert_recognizes, to see what they do.

Ruby on Rails - undefined local variable or method - wice grid haml show_code

I am new to ruby on rails.
I am trying to get haml and wice_grid to work together. I am using this example as a model:
http://wicegrid.herokuapp.com/basics3
I get the error 'undefined local variable or method `show_code' for...'
In the file app/views/basics3/index.html.haml which you can see at the link above.
Am I missing a gem? In general, what is the best way to troubleshoot problems like this?
Thanks in advance-
Flex
EDIT: I found the definition for show_code. It's in a helper that I found in the unit tests for wice_grid.
https://github.com/leikind/wice_grid_testbed/blob/master/app/helpers/application_helper.rb
That said, I get more errors when I load it into my project. So the question becomes, how does the helper normally get included in my project?
show_code is a custom method created just for the example page you linked to. It just displays the code he has in his controller and his index and grid views. You don't need to call that method in your own application so just remove that line and you should be good.

How to namespace routes in Rails 4 to set up Forem?

I've been trying to set up Forem (a Rails 4 forum engine) using a guide and original docs.
Most things work, but I'm getting route errors. In my application.erb I have this route in a link_to:
topic_path(u)
The guide recommends that I preface this with my application name so my routes won't conflict with Forem's routes, so I did that as so:
H2le.topic_path(u)
(H2le is the application name set in application.rb)
However, this errors out:
"undefined method `topic_path' for H2le:Module"
Am I not setting the application name properly?
The problem was my being a Ruby newb and the guide I was following perhaps not being super explicit. It recommended to namespace the links like:
main_app.path
And I interpreted main_app to be a placeholder for my app name. Well, wrong. main_app is a built-in helper function, so it should literally just say main_app. I fixed this, and everything worked.

undefined local variable or method `sign_out_path'

I'm beginner in rails application. I have used devise gem to authentication purpose.
when I log in its showing error:
undefined local variable or method `sign_out_path'
How can I solve this problem?
There could be two reasons for this:
You have not got the correct routes defined in your routes configuration file
You have used a path helper for an existing route but mistakenly used the wrong name
First run rake routes. Have a look through the output and see if you can see any routes beginning with "devise".
If you can see one called "destroy_user_session" then this is actually the name you need to use for your sign out link, and not "sign_out_path". In that case, go to the view where you have put your sign out link and replace the helper with "destroy_user_session_path".

Random method named cv_member_url()

I'm a total noob to rails and I'm trying to understand a project I'm working on. I've found a method named cv_member_url in one of the views, but I can't for the life of me figure out where it is defined... One thing I do know about rails is that it is a very flexible language so it could be some sort of gem creating this method.
Any ideas where this method may have come from? (or better yet, how I can add others)
Thank you!
Do you have a model called CvMember? If so, the method is probably a named route for that model. See here for more info:
http://guides.rubyonrails.org/routing.html#paths-and-urls
To see all your named routes, you can run
rake routes
Those are named routes which are automatically defined based on what you have in routes.rb. *_url should be used in the controller, and *_path should be used in the views. Here's some more info from the official rails guide.
Assuming you can run this in development: You should put a breakpoint on it and step inside. Most likely it is dynamically defined or maybe in plugin.

Resources