Deep beginning

Hey guys, it’ll be kinda silly question, but I was working for two years on my bachelor’s thesis using RoR…but I have never found out, how to do that when I type in the browser the address and port, there will be a homepage shown…is there some easy way?

Thanks in advance Jan Kadera

You mean setting your routes so that you can tell Rails where your homepage is at when you preview using localhost?

In rails 3, do something like this:

# Home Page Route   root :to => "site#home"

  # Site Routes   match '/', :to => "site#home"

So when you go to http://localhost:3000, rails will route to your homepage.

I’ve got rails 2.1 unfortunately. Isn’t there any more peaceful way to do it without declaring war to half of the application by changing routing?

btw, my app is available on the address tinyurl.com/skurt, please feel free to check it out. I have made there two controllers: home and Rolldance (rolldance is from some kind of turorial) home have bunch of actions, but there is view only for action wow rolldance have actions both with views index and o_nas What I’m trying to do is simply access those pages and I’m totally unsuccessful :frowning: Jan Kadera

What *are* you talking about?

You use routing to match URLs to the content you want to serve.

If you're not getting the results you expect, run `rake routes` to see what's actually configured.

Getting Started with Rails — Ruby on Rails Guides see section 4.2 Although its for 2.3, 2.1 looks to be similar.

“You use routing to match URLs to the content you want to serve.” Are you telling me, that the default routing setting do not work like this: my ip: 1.2.3.4 my port: 3000 controller name: c action name: a this implies the address http://1.2.3.4:3000/c/a.html ???

I’m asking, because I believe it work like that which means, when I start to change it, I’ll mess the whole app up.

Jan.

“If you’re not getting the results you expect, run rake routes to see what’s actually configured.”

skurt@pivnik:~/www/skurt$ rake routes (in /home/skurt/www/skurt) /:controller/:action/:id
/:controller/:action/:id.:format

Now I see I really don’t want to mess with it :frowning:

Jan

Well, after a while trying I found a way to do it right. The first time, I created just controller script/generate controller home and then created actions by editing the file app/controllers/home_controller like this: def action1 end def action2 end … and for every action I created a file like this app/views/home/action.html.erb and it didn’t work (that’s the point where you were suggesting routing)

but the only thing I needed was to destroy all the work done and use the command script/generate controller home action1 action2…

and voi’la myIP:3000/home works now :slight_smile:

BUT! What I’m trying to figure out now is addressing inside of the app. I’m trying to set up a background for the page but the relative path starting from the route of the app doesn’t work and unfortunately the absolute path doesn’t work either. So where is the root of the relative path?

Jan

Well, after a while trying I found a way to do it right. The first time, I created just controller

and it didn't work (that's the point where you were suggesting routing)

but the only thing I needed was to destroy all the work done and use the command script/generate controller home action1 action2...

and voi'la myIP:3000/home works now :slight_smile:

because the routes.rb file has been updated, which you could have easily done manually :slight_smile:

BUT! What I'm trying to figure out now is addressing inside of the app. I'm trying to set up a background for the page but the relative path starting from the route of the app doesn't work and unfortunately the absolute path doesn't work either. So where is the root of the relative path?

Static resources are typically served from /public/* so e.g. an image in {RAILS_ROOT}/public/images/foo.png would be accessed by the URL http://hostname:port/images/foo.png

HTH,

Thanks a lot :slight_smile: Jan

In the tutorial here: http://guides.rubyonrails.org/layouts_and_rendering.html a have found a problem. There is stated: “By default, the file is rendered without using the current layout. If you want Rails to put the file into the current layout, you need to add the :layout => true option.” …but where? I tried the view file and the controller file and neither doesn’t work.

Jan

Well, I figured now, that there is really no need to tell the controller to use the layout, it makes it by default, BUT…I’m still unable to set up the wallpaper. I moved the picture to the directory public/pic/ and add the address pic/darkKnight_wide.jpg to the layout file. But it only affected the index view file, in others there is still the same link for the background, but the background isn’t shown.

Well, I figured this one out now also…apparently there is huge difference between those two strings: /pic/picture.jpg pic/picture.jpg Which I guess means there are no relatives paths in rails, right?

Jan

Absolute and relative paths are indeed very different. This doesn't have much to do with rails though - it's just the way that html works. What do you mean by 'there are no relative paths in rails' ?

Fred

It's better to use the image_tag helper than code full paths to images in your app. The convention is like this:

1. create an images subdirectory under public in your rails app 2. link to an image via the image_tag helper

So in your case you'd create public/images/picture.jpg and link to it in a template like <%= image_tag "picture.jpg" %>.

To Frederick: “What do you mean by ‘there are no relative paths in rails’ ?” As I understand the difference, there is some point in the computer directory tree, where is said I’m working, for example /home/myusername/RubyApps/rails/default now if I want to link something inside of that directory, you just type name of the file, because some kind of magic or more than human power will know it should first look there. On the other hand the absolute path is recognized by starting with the only root of the whole computer filesystem, like “/” or "C:" …so if I have to use “/” in the pseudo-relative path like this, then I guess I’m using absolute path from the relative point described above, which I absolutely don’t get. And if it really is either of those, I can’t imagine usage of the other one. To cageface: Sir, I may be an idiot, but it doesn’t work. I have pictures in public/pic the picture filename is darkKnight_wide.jpg and I’m reffering to it like this:

> and I know nothing abou html, but there is no way that the 'higher power' translating that ruby babbling into html can know which subdirectory in the public directory it should try. To everyone: I'm trying to follow this tutorial to create a password checker in my app: [http://www.devarticles.com/c/a/Ruby-on-Rails/Login-Systems-and-More-with-Ruby-on-Rails/](http://www.devarticles.com/c/a/Ruby-on-Rails/Login-Systems-and-More-with-Ruby-on-Rails/) I followed until like two thirds of article and crashed on this one: "Create an entry in the users table, start the server, and you'll find that you can log in from *[http://localhost:3000/user/login](http://localhost:3000/user/login)*, and view your account information from *http:// localhost:3000/user/my_account*. " a created a login name and password in the correct database table, but, when I type in to my browser myIP:3000/user/login it's giving an error message saying that it cant apply something on []. I almost completely don't know what exactly am I doing, but I looked into the database and found the password there...the exact password, which is I would say lame, isn't it? There should be hash or something (not ruby-hash, but you probably know). Because of that I'd say that this tutorial is kind of crapy and I wanted to ask anyone if you know about any better, I'd love to read it.

Regards Jan

cageface: Oh, I get it now, this awesome trick works, but you have to set you app up EXACTLY, like it is said…I don’t get those ‘features’ but thanks.

Jan

Almost - you're looking at it from the point of view of your computer whereas you need to be looking it at from the point of view of the web browser & web server: / doesn't mean the root of your hard drive, it means the root of your website, which for a rails app is your_rails_app/public. Relative paths are relative to the page the browser is currently sitting on, so if your browser is displaying http://foo.com/blah then an image with path 'pics/logo.png' will request http://foo.com/blah/pics/logo.png whereas '/pics/logo.png' will request http://foo.com/pics/logo.png

Fred

On the other hand the absolute path is recognized by starting with the only root of the whole computer filesystem, like "/" or "C:\"

No, "absolute path" in terms of a web app means a path starting with a "/" that indicates the base of your *web application's root*. It's usually better to use absolute paths, since the actual context of any given page or page fragment may not be obvious.

Sir, I may be an idiot, but it doesn't work. I have pictures in public/pic the picture filename is darkKnight_wide.jpg and I'm reffering to it like this: <body background=<%= image_tag "darkKnight_wide.jpg" %>>

Of course it doesn't work -- you ignored what you were told, which was to put it in /public/images, which is the path an image_tag will create for it. It's a convention. Rails has lots of them, and learning and adhering to them will make this all much easier :slight_smile:

and I know nothing abou html, but there is no way that the 'higher power' translating that ruby babbling

And if you think this whole thing is "ruby babbling" -- why are you even bothering?

That is indeed a bad way to do things. You might want to look at authlogic which is what a lot of people use these days. restful_authentication also used to be quite popular.

Fred

I have moved all pictures in the images directory and it works now for the pictures. It unfortunately doesn’t work for background, or I don’t know how to throw it.

And one little question…how do I align pictures inserted into web like this? I can’t google it.

Jan