Rails 3: I have controller name and action and need to get path and http verb for it - ruby-on-rails-3

In rails 3.2 I need to get path and http verb of rails endpoint by controller name and action name.

please from terminal of your computer write rake routes | grep matching_controller_name this will show the path of the related matching routes of that path with http-verb and action and controller.

Related

Why does rspec-rails skip the middleware?

I have a Rails app (3.2.12) that I wanted to add locale switching via HTTP Accept-Language header.
What I did to achieve that:
I added rack-contrib to my Gemfile:
gem 'rack-contrib', require: 'rack/contrib'
ran bundle install, added the middleware to my config/application.rb:
config.middleware.use Rack::Locale
and inspect the request env my controller:
puts request.env.keys.select{|v| v=~/rack/ }
The spec I run is a controller spec, it has render_views in it.
My problem:
There's no rack.locale key in the request environment. I double-checked rake middlware, it lists Rack::Locale toward the end, right before run MyApp::Application.routes.
After some debugging I found out that the middleware is never called when I run
rspec spec/controllers/authentication_controller_spec.rb
BUT: Running the same code in script/rails s thin gives me more keys in the request env, namely:
rack.request.cookie_string
rack.locale
rack.request.query_string
rack.request.query_hash
So, I guess the question is: Why does RSpec refuse to pick up a Rack middleware?
Controller specs do not go through the stack, they pretty much call directly on the controller itself. You'll probably want to use Rspec's request type tests for this.

Cucumber: undefined method error for path on visit

I have a cucumber step
When /^I go to the Add Suggestions form$/ do
visit new_manage_suggestions_path
end
and a route
namespace "manage" do
resource :suggestions
end
rake routes outputs
manage_suggestions POST /manage suggestions(.:format) manage/suggestions#create
When I run cucumber I get
undefined method `suggestions_path' for #<#<Class:0x000000064a4768>:0x000000064accd8> (ActionView::Template::Error)
Why is cucumber trying that path?
The new_manage_suggestions_path works fine in my app, I have a link that uses it and that is working fine.
In your routes definition, in order to have your app generate the correct routes, you need to switch from the singular resource to plural resources since you could potentially have multiple suggestions.
namespace "manage" do
resources :suggestions
end
More details can be found in the Rails documentation on singular resources, where you can see that the singular version does not include the namespace in its path names.

Action controller: Exception Caught while trying to acces recently generated controller on empty Ruby on Rails project

I am new to ruby on rails, just trying to follow some tutorials.I just entered:
rails generate controller helloWorld index
then i started rails server and tried to access http://localhost:3000/hello_world/index
and got
Action controller: Exception Caught
ActiveRecord::ConnectionNotEstablished
ActiveRecord::ConnectionNotEstablished
in the browser
Did you run rake db:create db:migrate before starting the server.
If you did, pls check your database configuration in config/database.yml. If you can't figure it out, please let us know the contents of that file.

How to access Rails 3 engine models in rails console

In the mail app's rails console (irb), how to access engine's models.
update: Say "team" is my main app and "team_page" is the engine. "team_page" is required in main app in the gemfile through 'gem => "team_page", :path => "local/path/to/team_page"'.
when I go to team's rails console, I couldn't access team_page's models.
First you must know the module's name. To help with that, you can run a
bundle show team_page
to find its directory and explore over there (probably under lib/team_page.rb) until
you see the following definition:
module TeamPage
# ...
end
Let's say that the module is called TeamPage. Then just prepend double colon to its name like this:
::TeamPage::SomeModel.some_method

can't make a page root

I am a noob in ruby on rails so excuse this question if it is a stupid one :
I've added in my routes.rb the command :
root :to => "pages#home"
a have generated a pages-controller using
rails generate controller pages
in my pages_controller.rb i've defined a function named home :
def home
#text = "da"
end
in the views folder from my app in the newly generated pages folder i've created a file named home.html.erb . in it i've placed the following command :
<%= #text %>
The problem is that when i start the server my app isn't rooted in home and even when i run
localhost:3000/pages/home it still doesn't work :
No route matches "/home"
You have created a controller but no view, try these commands
$ rails new myapp
$ cd myapp
$ rails g controller pages index <- this will create a controller **plus** an index view
$ rm public/index.html
now edit routes.rb and add root :to => "pages#index
$ rails s
open http://localhost:3000 and you'll see the new page.
I'm a noob too (I started coding in ruby 2 weeks ago)... I strongly suggest you to get a book, such as Agile Web Development with Rails 4th ed. I covered the book in a week and it gave me a LOT of insight about ruby and rails.