Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
craisins
May 17, 2004

A DRIIIIIIIIIIIIVE!
Hey everyone, I recently just installed Ruby, Rails, and MySQL to my home machine and I'm trying to get into development. I'm using this: http://www.tutorialspoint.com/ruby-on-rails-2.1/index.htm tutorial and I've gotten up to making the "book" controller but whenever I go to localhost:3000/book/list or localhost:3000/book i get a routing error saying there's no route to that page. I don't know what I'm doing wrong. :(

Adbot
ADBOT LOVES YOU

jonnii
Dec 29, 2002
god dances in the face of the jews
Your controller should be called books_controller.

skidooer
Aug 6, 2001

walnutz posted:

I don't know what I'm doing wrong. :(
Do you have at least the default route in your routes.rb file?

Also, that tutorial doesn't seem to follow any of the newer Rails 2.1 conventions. It looks like it was written for 1.0. I would be weary of picking up bad habits from it. Unfortunately, I cannot suggest a better one for you to follow.

craisins
May 17, 2004

A DRIIIIIIIIIIIIVE!

skidooer posted:

Do you have at least the default route in your routes.rb file?

Also, that tutorial doesn't seem to follow any of the newer Rails 2.1 conventions. It looks like it was written for 1.0. I would be weary of picking up bad habits from it. Unfortunately, I cannot suggest a better one for you to follow.

All I had to do was apparently just restart the server. I don't know why.

Is there a book that covers the latest version of Ruby/Rails? I don't mind picking a book up.

hmm yes
Dec 2, 2000
College Slice
You have to restart your server any time you change your routes file.

I don't know if there are any Rails 2.0+ friendly books that have been published recently. I really enjoyed many of the PeepCode screencasts. You're probably most interested in Rails 2 From Scratch (Part 1, Part 2). Their rSpec, Passenger, and Capistrano videos are all great too.

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense

atastypie posted:

You have to restart your server any time you change your routes file.

That is false, you do have to restart your server anytime you change your environment.rb, database.yml, or vendor directory. Routes can be altered on the f.l.y.

skidooer
Aug 6, 2001

Nolgthorn posted:

That is false
Not exactly. There was a certain version of Rails that had a route reloading problem. But, if I recall correctly, that revision also contained a security problem, so nobody should be using it now anyway.

hmm yes
Dec 2, 2000
College Slice
Weird, I swear that when I started out with 1.6 that you had to restart your server to get the routes file to reload. That, plus comments like '# Be sure to restart your web server when you modify this file.' in environment.rb made me just assume anything in /config required a restart. I've also done a lot of work with Radiant, and any routes found in an extension require a restart in order to be found. Combine those two and I guess I learned a bad (useless?) habit.

Hammertime
May 21, 2003
stop

atastypie posted:

Weird, I swear that when I started out with 1.6 that you had to restart your server to get the routes file to reload. That, plus comments like '# Be sure to restart your web server when you modify this file.' in environment.rb made me just assume anything in /config required a restart. I've also done a lot of work with Radiant, and any routes found in an extension require a restart in order to be found. Combine those two and I guess I learned a bad (useless?) habit.

I believe the restarting for routes requirement was only removed in 2.0+, though I could be wrong on this.

You also have to restart if you make any modification to files in lib.

I've got a bad habit, I restart my development mongrel waaaay too much.

skidooer
Aug 6, 2001

Hammertime posted:

You also have to restart if you make any modification to files in lib.
No you don't, unless you explicitly require the file that is. But apparently 2.2 really does have a problem with routing to new controllers without a restart. I just encountered it.

Hammertime
May 21, 2003
stop

skidooer posted:

No you don't, unless you explicitly require the file that is. But apparently 2.2 really does have a problem with routing to new controllers without a restart. I just encountered it.

Well, yes, but what's the point in having lib files if you don't require any of them?

skidooer
Aug 6, 2001

Hammertime posted:

Well, yes, but what's the point in having lib files if you don't require any of them?
Rails will load them automatically. You may have to specify additional load paths if your lib files are in a directory hierarchy.

skidooer
Aug 6, 2001
How do you guys do "admin sections" of an application? All of the solutions I have come up with / have seen leave a lot to be desired; either by way of duplication of efforts, ugly code, or what have you. There much be a better way.

Hop Pocket
Sep 23, 2003

skidooer posted:

How do you guys do "admin sections" of an application? All of the solutions I have come up with / have seen leave a lot to be desired; either by way of duplication of efforts, ugly code, or what have you. There much be a better way.

I usually use a base controller that all of my admin controllers inherit from, in addition to an 'admin' namespace. The base controller will use a before_filter that will require a logged in user with admin? privileges. That will take care of most of the duplication right off the bat. In addition, I normally use a pretty slim layout just to make things efficient. Also, you can use an Admin:: namespaced helper just for your admin-related shennanigans.

There are some times where you can work the admin interface into the site itself. I've found that to be a poor solution, especially for any more than the simplest of admin interfaces.

mynameisntneo
Sep 12, 2006
Mr. Anderson
I've been using Rails for a while, but I want to know the "correct" way to do this (using RESTful routes?):

I have three models, Menu, MenuItem, and MenuMember.

Menu(:id, ...)
MenuMember(:id, :menu_id, :menu_item_id, :is_default, ...)
MenuItem(:id, ...)

Menu has_many :menu_members
Menu has_and_belongs_to_many :menu_items, :through => :menu_member

I want to have an edit view in MenuConroller where you can add MenuItems to it. It needs to be more complex than just a multiselect - I want to to be able to edit the MenuMember objects along with the Menu on the same page. Is the way to do this by setting up:

map.resources :menu, :has_many => :menu_members

So I can call /menu/1/menu_members/new/, etc. and then in the MenuMember controller have each action check for params[:menu_id]? That seems a little clunky. Is there a best way to do what I'm looking for?

skidooer
Aug 6, 2001

Hop Pocket posted:

That will take care of most of the duplication right off the bat.
The duplication I was referring to was how the admin and main site controllers end up being almost exactly the same, only different. Different enough that using one controller to serve both views doesn't work well either. Writing the application twice, so to speak, gets old. Maintaining the site twice is even worse.

mynameisntneo posted:

Is there a best way to do what I'm looking for?
You will want to use a before_filter.
code:
class MenuMemberController < ApplicationControllerr
  before_filter :set_menu
  
  def index
    @menu_members = @menu.menu_members.find(:all, :limit => 10)
  end
  
  private
    def set_menu
      @menu = Menu.find(params[:menu_id])
    end
end

skidooer fucked around with this message at 05:22 on Dec 4, 2008

Hop Pocket
Sep 23, 2003

skidooer posted:

The duplication I was referring to was how the admin and main site controllers end up being almost exactly the same, only different. Different enough that using one controller to serve both views doesn't work well either. Writing the application twice, so to speak, gets old. Maintaining the site twice is even worse.

Well, ok. How about using something like resource_controller to cut down on the controller cruft, assuming you're OK with REST, and then incorporate the scaffolding views into your admin area so that you don't have to keep up with each maintained field. If you query the model for its attributes and then iterate over them, creating form fields for those attributes, then that takes a lot of the drudgery out of it and your forms grow as your models do.

phazer
May 14, 2003

chirp chirp i'm a buffalo
Question about adding Restful Authentication to a Rails 2.2 app:


I followed the tutorial here http://www.avnetlabs.com/rails/restful-authentication-with-rails-2

But the guy doesn't say how to actually protect the controllers you want to require login for.

Anyone know how?

Trabisnikof
Dec 24, 2005

phazer posted:

Question about adding Restful Authentication to a Rails 2.2 app:


I followed the tutorial here http://www.avnetlabs.com/rails/restful-authentication-with-rails-2

But the guy doesn't say how to actually protect the controllers you want to require login for.

Anyone know how?

I'd guess a :before_filter in your application controller, but I'm not at work so that name might be wrong.

sorghum
Jul 9, 2001

Trabisnikof posted:

I'd guess a :before_filter in your application controller, but I'm not at work so that name might be wrong.
Yeah, it's before_filter :login_required in whatever controllers you want to protect. You can also use the :except and :only options to limit it to certain actions - for example before_filter :login_required, :except => [:index, :show].

phazer
May 14, 2003

chirp chirp i'm a buffalo

sorghum posted:

Yeah, it's before_filter :login_required in whatever controllers you want to protect. You can also use the :except and :only options to limit it to certain actions - for example before_filter :login_required, :except => [:index, :show].

When I use before_filter :login_required I get an undefined method error.

code:
NoMethodError (undefined method `login' for #<BandsController:0x412bf8f4>):
I followed that tutorial exactly. Could it have anything to do with my version of Rails? (2.1.2)

edit:

OK I don't know what's going on, but I am doing this on Media Temple hosting, and I thought well maybe I should restart the application, maybe that will get some of my changes to take effect. So I went to restart it and the mongrel.pid file got deleted and now my drat application won't start.

Media Temple support was no help last time I had this problem. (we don't support custom code, etc. well what's the point then?) Can anyone help?

phazer fucked around with this message at 01:19 on Dec 7, 2008

sorghum
Jul 9, 2001
I've actually only used an old version of Restful Authentication, so I'm not sure what's happening there. Could you post the stack trace?

Check the log files if you can't start the application. It shouldn't have anything to do with the mongrel.pid file though - that file is only supposed to exist while the application is running.

Operation Atlas
Dec 17, 2003

Bliss Can Be Bought

phazer posted:

When I use before_filter :login_required I get an undefined method error.

code:
NoMethodError (undefined method `login' for #<BandsController:0x412bf8f4>):
I followed that tutorial exactly. Could it have anything to do with my version of Rails? (2.1.2)

edit:

OK I don't know what's going on, but I am doing this on Media Temple hosting, and I thought well maybe I should restart the application, maybe that will get some of my changes to take effect. So I went to restart it and the mongrel.pid file got deleted and now my drat application won't start.

Media Temple support was no help last time I had this problem. (we don't support custom code, etc. well what's the point then?) Can anyone help?

1) I have a feeling that in your action you are trying to muck with a "login" variable that is undefined- if there is no local variable defined, it will look for it in the next best place- a method on your BandsController instance (where it can't find it because it doesn't exist, so it tosses).

The important thing to remember here is that Rails controllers aren't magic. Your "actions" are just methods in a class. Each action is an instance method, and "self" inside of actions is the instance of your controller class. (If that doesn't make sense, Google "Object-Oriented Programming") Try to think of what you're doing inside of a Rails action as a normal method call, in a normal class only your return is a little weird (You are quite literally returning your render/redirect method call).

2) The media temple stuff has nothing to do with this. You should be doing all of this stuff locally anyways. Only once you get a working application should you be deploying to a webhost. For some reason I'm assuming you're on windows- don't they have one-click Rails installs and whatnot? Use those.

dustgun
Jun 20, 2004

And then the doorbell would ring and the next santa would come
edit: nevermind

dustgun fucked around with this message at 15:24 on Dec 10, 2008

phazer
May 14, 2003

chirp chirp i'm a buffalo

Praetorian42 posted:

1) I have a feeling that in your action you are trying to muck with a "login" variable that is undefined- if there is no local variable defined, it will look for it in the next best place- a method on your BandsController instance (where it can't find it because it doesn't exist, so it tosses).

The important thing to remember here is that Rails controllers aren't magic. Your "actions" are just methods in a class. Each action is an instance method, and "self" inside of actions is the instance of your controller class. (If that doesn't make sense, Google "Object-Oriented Programming") Try to think of what you're doing inside of a Rails action as a normal method call, in a normal class only your return is a little weird (You are quite literally returning your render/redirect method call).

2) The media temple stuff has nothing to do with this. You should be doing all of this stuff locally anyways. Only once you get a working application should you be deploying to a webhost. For some reason I'm assuming you're on windows- don't they have one-click Rails installs and whatnot? Use those.

I did have a working application before I deployed. In fact, it was working AFTER deployment until this restart issue. It is Media Temple. They said my Mongrel gem is corrupt and they don't know why. And I'm on OS X.

Operation Atlas
Dec 17, 2003

Bliss Can Be Bought

phazer posted:

I did have a working application before I deployed. In fact, it was working AFTER deployment until this restart issue. It is Media Temple. They said my Mongrel gem is corrupt and they don't know why. And I'm on OS X.

Well, to be sure, the mongrel pid problem and the nomethoderror problem are two different problems. If it is working locally but not working remotely on the same code, all I can say is make sure you've run your migrations.

The chances of your mongrel gem being corrupt are slim to none. The mediatemple people have no idea what they're talking about. How did you do the restart? Is this a full-on VPS or are you stuck on some shared thing?

skidooer
Aug 6, 2001

phazer posted:

I did have a working application before I deployed. In fact, it was working AFTER deployment until this restart issue.
Have you diffed the source on the server with the deployed revision? Perhaps a colon accidentally disappeared from a symbol, for example. Then, when you restarted your application the new code was loaded into memory and boom, your code was then broken.

skidooer fucked around with this message at 22:56 on Dec 10, 2008

phazer
May 14, 2003

chirp chirp i'm a buffalo

skidooer posted:

Have you diffed the source on the server with the deployed revision? Perhaps a colon accidentally disappeared from a symbol, for example. Then, when you restarted your application the new code was loaded into memory and boom, your code was then broken.

Well, since on MT, the app runs in Production mode, I figured my problem was I needed to restart the app after making changes to controllers and what-not. I went to restart my app, and the mongrel.pid issue happened. Regardless, it's all useless until they fix it. They escalated me to higher-tier support.

Thanks for all the help so far, though!

skidooer
Aug 6, 2001

phazer posted:

I went to restart my app, and the mongrel.pid issue happened.
Run this:
code:
ps -A | awk '/mongrel/ {print $1}' | xargs kill
Start your Rails application(s) again.

phazer
May 14, 2003

chirp chirp i'm a buffalo

skidooer posted:

Run this:
code:
ps -A | awk '/mongrel/ {print $1}' | xargs kill
Start your Rails application(s) again.

I got:

code:
Usage:
  kill pid ...              Send SIGTERM to every process listed.
  kill signal pid ...       Send a signal to every process listed.
  kill -s signal pid ...    Send a signal to every process listed.
  kill -l                   List all signal names.
  kill -L                   List all signal names in a nice table.
  kill -l signal            Convert between signal numbers and names.

skidooer
Aug 6, 2001
And what did you get when you started your application?

Operation Atlas
Dec 17, 2003

Bliss Can Be Bought

phazer posted:

I got:

code:
Usage:
  kill pid ...              Send SIGTERM to every process listed.
  kill signal pid ...       Send a signal to every process listed.
  kill -s signal pid ...    Send a signal to every process listed.
  kill -l                   List all signal names.
  kill -L                   List all signal names in a nice table.
  kill -l signal            Convert between signal numbers and names.

Just do
code:
ps aux | grep mongrel
, find the PID of your mongrel, and then kill it. Then start it again.

phazer
May 14, 2003

chirp chirp i'm a buffalo

skidooer posted:

And what did you get when you started your application?

code:
$ mtr start cylsrecords
Error executing command:
application failed to start, check /home/38340/containers/rails/cylsrecords/log/mongrel.log for errors
return code: 8
output: /home/38340/data/rubygems/gems/gems/mongrel-(same line)
1.1.5/bin/../lib/http11.so: libruby1.8.so.1.8: cannot open shared object file: (same line)
 No such file or directory - /home/38340/data/rubygems/gems/gems/mongrel- (same line)
1.1.5/bin/../lib/http11.so (LoadError)
(I added the (same line) to indicate it's all one line, but it was breaking tables)

When I grep for a mongrel process, I get no results.

phazer
May 14, 2003

chirp chirp i'm a buffalo
Can someone point me in the right direction for doing multiple file uploads in Rails2? I want to have 1 upload field with the ability to add more through AJAX. I'm googling but finding nothing valuable.

skidooer
Aug 6, 2001

phazer posted:

When I grep for a mongrel process, I get no results.
Try:
code:
gem pristine mongrel
And then start your application again.

phazer
May 14, 2003

chirp chirp i'm a buffalo

skidooer posted:

Try:
code:
gem pristine mongrel
And then start your application again.

Same exact error. Like I said, MT support is on it... just waiting to hear back.

Operation Atlas
Dec 17, 2003

Bliss Can Be Bought

phazer posted:

Same exact error. Like I said, MT support is on it... just waiting to hear back.

Ugh. I'd ditch MT if I were you. I had a deal for free MT hosting for a year and after 6 months decided to pay for hosting rather than keep using them. They're really quite awful.

Get a $20 account at Slicehost. Sure it might be a bit more, but you'll be saving yourself so much time by not having to deal with the crap that MT forces you through.

hmm yes
Dec 2, 2000
College Slice
Do you mean MT in general, or MT specifically for Rails?

Operation Atlas
Dec 17, 2003

Bliss Can Be Bought

atastypie posted:

Do you mean MT in general, or MT specifically for Rails?

MT for Rails. If you use PHP, I'd imagine it would be just fine.

Adbot
ADBOT LOVES YOU

phazer
May 14, 2003

chirp chirp i'm a buffalo

Praetorian42 posted:

Ugh. I'd ditch MT if I were you. I had a deal for free MT hosting for a year and after 6 months decided to pay for hosting rather than keep using them. They're really quite awful.

Get a $20 account at Slicehost. Sure it might be a bit more, but you'll be saving yourself so much time by not having to deal with the crap that MT forces you through.

I will say deploying an existing rails app on Media Temple was WAY easier than dreamhost. (before i decided to restart the application)

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply