MatthewCampagna
MatthewCampagna

Reputation: 1

Ruby on Rails: Not loading views as expected

I am new to Rails (and this site) and working through a Lynda.com tutorial. I have run into many issues because the tutorial was made about 3 years ago an it seems that the programming language has changed along the way. Luckily I have figured out how to get around most of the issues, but there is one particular one that keeps me from moving forward.

Versions:

Ruby 2.0.0p195

Mysql 14.14 dis 5.6.12

Gem 2.0.3

I am trying to connect to different .html.erb pages in my demo folder, but they are not being recognized by the def in my script.

visual examples here: https://dl.dropboxusercontent.com/u/56018487/rubyexample.png

It is my understanding that the "def hello" and "def other_hello" should automatically look for hello.html.erb and other_hello.html.erb files in my demo folder under views in my application. However, when I run the server and type those locations in the address bar, Firefox does not find them using localhost:3000/demo/hello or localhost:3000/demo/other_hello. I am sure it is something really easy, but I have not found the answer after 2 days of web searches.

As part of my trouble shooting efforts, I have tried each individual variation under the "def index" section that has been commented out. I can get to each page from the "def index" section when not commenting out get "demo/index", but not from the "def hello" or "def other_hello" sections.

Update: It is my goal to get a dynamic answer to my issue. In Rails 2, there is an elegant, single line of code that handles all incoming information.

I hope this makes sense. Please let me know if you have suggestions.

A shout out to zeantsoi and Muntasim for your help in this matter. I would give each of you a point, but the site does not allow me to do that.

Upvotes: 0

Views: 537

Answers (2)

zeantsoi
zeantsoi

Reputation: 26193

You are missing routes to your DemoController actions. Add them like this:

# config/routes.rb
match 'demo/hello', 'demo#hello'
match 'demo/other_hello', 'demo#other_hello'

With these routes in place, you can access the hello and other_hello actions by visiting the paths demo/hello and demo/other_hello, respectively.

One other thing you may consider is adding a name to your route, which vastly facilitates routing from controllers and views:

# config/routes.rb
match 'demo/hello', 'demo#hello', :as => demo_hello

Then, in your view (or controller), you can use the following:

demo_hello_path #=> /demo/hello
demo_hello_url #=> hostname/demo/hello

EDIT:

If you're looking to dynamically route to your controller and action, you can use the following match pattern:

# config/routes.rb
match ':controller/:action'

Be warned that depending on what order this route is executed, it may override (or be overridden by) other hardcoded routes.

As an FYI, the section on dynamic segments from the official Rails routing guide is helpful in figuring out how things are mapped.

Upvotes: 1

Muntasim
Muntasim

Reputation: 6786

it seems you didnt define you routes yet. you can make sure by runnng rake routes and see if your routes are there. Otherwise define routes using:

# config/routes.rb
match 'demo/hello', 'demo#hello'
match 'demo/other_hello', 'demo#other_hello'

or simply:

get 'demo/hello'
get 'demo/other_hello'

Upvotes: 2

Related Questions