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
The March Hare
Oct 15, 2006

Je rêve d'un
Wayne's World 3
Buglord
Oh boy, I hadn't even thought about deployment until now. I have the base of this little tiny app done and, well, as the final step in learning Ruby I'm going to have to deploy it. I have never ever deployed a web app ever. The most advanced thing I've ever done is uploading PHP files as a straightshot and setting up sql databases for them - which is easy. The book I've got right now wants me to make an svn account and use capistrano and phusion but... I can't install phusion on my webhost so I'm assuming I can't do that (unless I'm missing how it works). All I can see that is rails related in my cpanel is the RoR thing that lets you make apps and a gem installer that wont let me install phusion.

So... what is the easiest way for me to go about deploying this thing?

(If it makes any difference I'm on a mac.)

Adbot
ADBOT LOVES YOU

NotShadowStar
Sep 20, 2000

Insurrectum posted:

I'm new to web programming and have some general programming experience but I wouldn't say I'm extraordinarily adept. Mostly Numerical Recipes C++-type programming dealing with just numerical analysis.

I want to make a simple webapp that just does the following:

Take a query.
Analyze it.
Graph the output.

All I'm wondering is the relative difficulty and time it will take to implement this, and maybe some suggestions of any graphing packages to use.

PHP might actually work for you for such a simple thing. It'd be far closer to C++ than you're used to, Ruby is more like Smalltalk+Perl and not really like anything else. Rails is good for build applications. If you want to use Ruby, I'd suggest Sinatra instead of Rails proper.

For your graph, I'd suggest creating the data server-side, turning it into JSON (ActiveRecord makes this really easy with .to_json) and using flot client-side to turn it into a graph.

NotShadowStar
Sep 20, 2000

The March Hare posted:

Oh boy, I hadn't even thought about deployment until now. I have the base of this little tiny app done and, well, as the final step in learning Ruby I'm going to have to deploy it. I have never ever deployed a web app ever. The most advanced thing I've ever done is uploading PHP files as a straightshot and setting up sql databases for them - which is easy. The book I've got right now wants me to make an svn account and use capistrano and phusion but... I can't install phusion on my webhost so I'm assuming I can't do that (unless I'm missing how it works). All I can see that is rails related in my cpanel is the RoR thing that lets you make apps and a gem installer that wont let me install phusion.

So... what is the easiest way for me to go about deploying this thing?

(If it makes any difference I'm on a mac.)

What's your webhost's Rails support? Many of them already use Passenger because it's easy to put into Apache like PHP is.

Pardot
Jul 25, 2001




The March Hare posted:

So... what is the easiest way for me to go about deploying this thing?

heroku, heroku, a million times heroku

The March Hare
Oct 15, 2006

Je rêve d'un
Wayne's World 3
Buglord

NotShadowStar posted:

What's your webhost's Rails support? Many of them already use Passenger because it's easy to put into Apache like PHP is.

I have no idea, I just know that passenger isn't listed in the ruby gems 'installed' list. How would I find out?

e; For reference, I know that I have apache. I also know that, within cpanel X, I can see a ruby on rails button that lets me create a skeleton app and I also have a ruby on rails gem manager thing that doesn't actually let me install things (such as passenger) despite them being there with an install button.

The March Hare fucked around with this message at 20:02 on Mar 26, 2010

NotShadowStar
Sep 20, 2000

The March Hare posted:

I have no idea, I just know that passenger isn't listed in the ruby gems 'installed' list. How would I find out?

e; For reference, I know that I have apache. I also know that, within cpanel X, I can see a ruby on rails button that lets me create a skeleton app and I also have a ruby on rails gem manager thing that doesn't actually let me install things (such as passenger) despite them being there with an install button.

Does the skeleton app function when you create it? If so just do that and overwrite it in full with one your app. You will need to change your database.yml file to reflect your host's MySQL server settings, if you use MySQL. Once you do that, you should be able to do rake db:migrate RAILS_ENV=production and restart your server (somehow, probably within cpanel) and you're off.

Or heroku.

The March Hare
Oct 15, 2006

Je rêve d'un
Wayne's World 3
Buglord
Trying this now, I have shell access too - I don't really know what my limits are but it seems to be letting me update things so that's good.

OK I tried to rake but that didn't work, it gave me this error:

(in /home/liarsbur/rails_apps/penny)
rake aborted!
undefined method `generate_best_match=' for ActionController::Routing:Module

Then I noticed that mongrel wasn't running and I tried to start that but it gave me this error:


e; "** Starting Mongrel listening at 0.0.0.0:3000
** Starting Rails with development environment...
Missing the Rails 2.3.5 gem. Please `gem install -v=2.3.5 rails`, update your RAILS_GEM_VERSION setting in config/environment.rb for the Rails version you do have installed, or comment out RAILS_GEM_VERSION to use the latest version installed."

I tried commenting out RAILS_GEM_VERSION - didn't solve it. I updated the gem to the right version (I think) and it is still broken :( Any ideas

ee; Just checked and my rubygems updater thing in cpanel agrees that I do, in fact, have "rails 2.3.5"

The March Hare fucked around with this message at 21:25 on Mar 26, 2010

NotShadowStar
Sep 20, 2000
Try 'rake rails:freeze:gems' which will put rails in your application instead of loading from system gems.

One of the great things about the design of Rails is that you can be (almost) independent of the system. Rails 3 lets you be completely independent of the system (Don't use Rails 3 yet)

The March Hare
Oct 15, 2006

Je rêve d'un
Wayne's World 3
Buglord
Um that ran fine but I still can't do any of the other things... :( - same errors.

skidooer
Aug 6, 2001

Pardot posted:

The server side option would be something built on rmajgick, but unless things have changed in the last year or so, everyone hates rmagick.
Your post has made me wonder how difficult it would be to update HotCocoa::Graphics to use Cocotron?

NotShadowStar
Sep 20, 2000
Wait, you froze rails and it still tells you it can't find rails? I've never seen that...

The March Hare
Oct 15, 2006

Je rêve d'un
Wayne's World 3
Buglord
code:
rake rails:freeze:gems
(in /home/liarsbur/rails_apps/penny)
Freezing to the gems for Rails 2.3.2
rm -rf vendor/rails
mkdir -p vendor/rails
cd vendor/rails
Unpacked gem: '/home/liarsbur/rails_apps/penny/vendor/rails/activesupport-2.3.2'
mv activesupport-2.3.2 activesupport
Unpacked gem: '/home/liarsbur/rails_apps/penny/vendor/rails/activerecord-2.3.2'
mv activerecord-2.3.2 activerecord
Unpacked gem: '/home/liarsbur/rails_apps/penny/vendor/rails/actionpack-2.3.2'
mv actionpack-2.3.2 actionpack
Unpacked gem: '/home/liarsbur/rails_apps/penny/vendor/rails/actionmailer-2.3.2'
mv actionmailer-2.3.2 actionmailer
Unpacked gem: '/home/liarsbur/rails_apps/penny/vendor/rails/activeresource-2.3.2'
mv activeresource-2.3.2 activeresource
Unpacked gem: '/home/liarsbur/rails_apps/penny/vendor/rails/rails-2.3.2'
cd -
[email]liarsbur@liarsburninhell.com[/email] [~/rails_apps/penny]# rake db:migrate RAILS_ENV=production
(in /home/liarsbur/rails_apps/penny)
rake aborted!
undefined method `generate_best_match=' for ActionController::Routing:Module

NotShadowStar
Sep 20, 2000
Aha, your host's Rails gem is 2.3.2 and you have a 2.3.5 app. I'd say freeze rails on your machine then transfer, then it should work fine. When you freeze it takes the currently installed system wide Rails and puts it in your application, which on your host is 2.3.2.

Lots of shared hosting providers don't know how to manage Rails and yours falls into that category. 2.3.2 is ooooooooold. Therefore, heroku. (I've been using Dreamhost for Rails apps since 2006 and they've been totally fine).

The March Hare
Oct 15, 2006

Je rêve d'un
Wayne's World 3
Buglord
OK, I must be a complete retard or something because this poo poo is just not working in any way at all. So I guess I'll have to use heroku or just get a new webhost. Is shared webhosting always so lovely when it comes to rails? Would I be better off just getting some lovely unmanaged VPS and throwing apache sql and rails on that and going from there or what?

Operation Atlas
Dec 17, 2003

Bliss Can Be Bought

The March Hare posted:

Is shared webhosting always so lovely

Yes. Rails and shared hosting is like trying to put a jet engine on your big wheels. Get a VPS (Linode) or use Heroku.

Pardot
Jul 25, 2001




Seriously just try heroku it's free until you need more power, at that point you can decide if you want to do take on the hassle of maintaining a server or just pay them.

It's literally two commands to deploy a new app to heroku.

The March Hare
Oct 15, 2006

Je rêve d'un
Wayne's World 3
Buglord
Aight' I'll give heroku a go later. This is just all so very frustrating because I'm obviously capable of making this poo poo work locally but getting it deployed so I can do bug testing with friends is just not happening and I really want to learn this goddamn language/platform. Thanks for the help NotShadowStar &al

Pardot
Jul 25, 2001




The March Hare posted:

Aight' I'll give heroku a go later.

I started, finished, and deployed this app since my last post, it's that easy: http://justuse.heroku.com/The%20March%20Hare

hmm yes
Dec 2, 2000
College Slice

Pardot posted:

I started, finished, and deployed this app since my last post, it's that easy: http://justuse.heroku.com/The%20March%20Hare

love it

The March Hare
Oct 15, 2006

Je rêve d'un
Wayne's World 3
Buglord

Pardot posted:

I started, finished, and deployed this app since my last post, it's that easy: http://justuse.heroku.com/The%20March%20Hare

Hahaha point taken.

Lamont Cranston
Sep 1, 2006

how do i shot foam
Holy crap, how had I not heard of Heroku before? This looks amazing.

jonnii
Dec 29, 2002
god dances in the face of the jews

Pardot posted:

I started, finished, and deployed this app since my last post, it's that easy: http://justuse.heroku.com/The%20March%20Hare

You should escape your inputs!! =)

Pardot
Jul 25, 2001




jonnii posted:

You should escape your inputs!! =)

ok done.

I cheated a bit and used Sinatra :ssh:

code:
require 'rubygems'
require 'sinatra'

get "/*" do |who|
 who = Rack::Utils.escape_html("#{who}, ") unless who.empty?
 "<h1>serioulsy, #{who}just use heroku</h1>"
end
is the entire app

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense
I decided to use a unique name column to look up objects on one of my models. I want to be able to change this unique name.

code:
class Page < ActiveRecord::Base
  validates_uniqueness_of :name
  validates_format_of :name, :with => /^[a-zA-Z0-9_]*$/

  def to_param
    name.parameterize
  end
end

- form_for(@page, :url => page_path(@page)) do |f|
  = f.text_field :name
  ..etc
If validation fails submitting this form to edit the name, path generated also changes.

code:
<form action="/pages/index_2" class="edit_page" id="edit_page_201" method="post">
Becomes.

code:
<form action="/pages/index invalid name" class="edit_page" id="edit_page_201" method="post">
This means the next time that I try and submit the form it directs me to the wrong place. How can I get it to behave the way I want, since it can't lookup any records with the invalid name.


Edit: hmm

I suppose I could use something like this.

code:
- form_for(@page, :url => page_path(Page.find(@page.id))) do |f|

Nolgthorn fucked around with this message at 02:15 on Apr 3, 2010

GroceryBagHead
Nov 28, 2000

Try skipping :url thing completely. Rails sometimes is smart enough to figure out it:

code:
- form_for @page do |f|

NotShadowStar
Sep 20, 2000
It's smart enough to figure it out if you tell it how to figure it out :)

You tell it how to figure it out by using REST and map.resources :page in your routes. Then rails knows exactly what to do whenever you call link_to @page, and you get other helper methods like pages_url, edit_page_url(@page) and such.

http://guides.rubyonrails.org/routing.html#restful-routing-the-rails-default

Also you're doing validations wrong. Don't generate another entirely different page just because a validation failed. You want a standard REST controller:

code:
class Pages<ActionController
  def index
    @pages = Pages.all    
  end
  
  def new
    if @page.create(params[:page])
      flash.notice = "New page created"
      # i usually go to index but this works too
      redirect_to page_url(@page)
    else
      flash.notice = "Couldn't save page"
      render :edit
    end
  end
  
  def update
    @page = Page.find(params[:id])
    @page.update_attributes(params[:page])
    if @page.save
      flash.notice "Page #{@page.name} updated"  
      redirect_to page_url(@page)
    else
      flash.notice "Couldn't update page"
      render :edit
    end
  end
  
  def create
    @page = Page.create(params[:page])
    if @page.save
      flash.notice = "Page #{@page.name} created"
      redirect_to page_url(@page)
    else
      flash.notice = "Page couldn't be saved"
      render :edit
    end
  end
  
  def edit
    @page = Page.find(params[:id])
  end
  
  def destroy
    @page = Page.find(params[:id])
    if @page.destroy
      flash.notice = "Page deleted"
      redirect_to :index
    else
      flash.notice = "Couldn't delete #{@page.name}"
      render :edit
    end
  end     
end
And you want your edit and new views to be:
code:
<% render :partial => 'form' %>
Move your Page form into _form and then at the top of that add
code:
<%= error_messages_for(@page) %>
And lastly change your validator to:

code:
validates_format_of :name, :with => /^[a-zA-Z0-9_]*$/, :message => "Name must be only numbers and letters"
Now you're all set. If someone enters in an invalid name, the controller will catch it and send the person to re-edit it. The Rails validation handling (error_messages_for) will show a happy bright red message box with your error messages (or however you style it) and highlight any invalid fields in red (or however you style them).

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense
I really appreciate it NotShadowStar. But I've merely simplified all that stuff you wrote, so that it would be easier to answer my more specific question.

I'm already using REST and standard restful controllers and error_messages_for and a form partial and those things. When validation fails, the page is refreshed. It doesn't really go to an entirely different page, the update action re-renders the edit page again. Even though a happy bright red message box does indeed appear the form's action parameter changes and points to an incorrect path based on the modified name field in the form.

This is because the name field is used to render the model object as a parameter in my model. But I don't really know how to fix it. The best thing would be if I could pick and choose times that to_param renders as the name, or renders as a id.

Nolgthorn fucked around with this message at 21:10 on Apr 3, 2010

drjrock_obgyn
Oct 11, 2003

once i started putting pebbles in her vagina, there was no going back

Nolgthorn posted:

This is because the name field is used to render the model object as a parameter in my model. But I don't really know how to fix it. The best thing would be if I could pick and choose times that to_param renders as the name, or renders as a id.

It doesn't feel too railsy but I've done that in the past by doing page_path(@page.id) for occasions where it needs the id and letting to_param take over in the instances where it doesn't.

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense
^^ This.

Totally answers my question, it's genius! I totally forgot model_path() accepts strings, integers and the like... this'll only take a bit of changes.

drjrock_obgyn
Oct 11, 2003

once i started putting pebbles in her vagina, there was no going back
Not a joke: Would any goons be interested in helping create a blog engine using rails 3 with a mongodb backend? I hesitate to say that the world needs another blogging engine but rails sure does. I really don't like any of the existing ones.

Kaedric
Sep 5, 2000

For someone just starting out with rails, do you guys know of any open-source projects or something similar to help someone get some practice using the language? I've been programming for almost 15 years and learning a new language isn't a big deal, but I definitely want to actually have some hands-on experience with this one to make sure I stick with it, and so that I have something to show prospective employers should I look for a rails job in the future.

Ghotli
Dec 31, 2005

what am art?
art am what?
what.
You could write a rails plugin and put it up on github. That or you could figure out and patch some of the bugs on the rails bug tracker. I know they're looking for help with that. You would look like a rails badass if you could say that you contributed to rails core.

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense
Hello.

The solution may be just out of my reach... I've slept on it, woken up and still remain confused about how to solve it. :wall:


I have three models

:page, :chunk and :cut_text

Chunk is a crazy model. It has two polymorphic associations

:chunkable and :cutable

:page is a :chunkable and :cut_text is a :cutable. :chunk is acting right now as sort of a habtm between :page and :cut_text.

:cut_text and :chunk are both created at the same time when creating a :cut_text.

:chunk contains a few nested attributes that appear on the :cut_text form.

When creating a new :cut_text;
'accepts_nested_attributes_for :chunk' allows :chunk attributes to be applied to a new :chunk.


I've added validation to :chunk which validates both :chunkable and :cutable have been applied.
code:
class Chunk < ActiveRecord::Base
  belongs_to :chunkable, :polymorphic => true
  belongs_to :cutable, :polymorphic => true

  validates_presence_of :chunkable_id, :chunkable_type, :cutable_id, :cutable_type
  attr_accessible :background_color, :background_image
{etc..}
end

class CutText < ActiveRecord::Base
  has_one :chunk, :as => :cutable, :dependent => :destroy

  attr_accessible :content, :chunk_attributes
  accepts_nested_attributes_for :chunk
{etc..}
end
In CutTextsController I call '@cut_text = CutText.new(params[:cut_text])' and '@cut_text.save' in 'def create'.


It is necessary I specify '@cut_text.chunk.chunkable = @page' somewhere as to ensure the new nested :chunk object contains a :chunkable object and passes validation before @cut_text can be saved successfully.

But the chunkable attribute is not accessible due to attr_accessible which is trying to prevent users from modifying the value.

I cannot call update_attribute after the record exists as the record will never validate.


How do I tell rails that it's ok to set the :chunkable attribute before saving the :chunk, while still using attr_accessible and validation?

Operation Atlas
Dec 17, 2003

Bliss Can Be Bought

Nolgthorn posted:

]
How do I tell rails that it's ok to set the :chunkable attribute before saving the :chunk, while still using attr_accessible and validation?

You can't. The solution is probably to just be careful in the controllers (setting the value in the hash yourself) and take out attr_accessible.

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense

Operation Atlas posted:

You can't. The solution is probably to just be careful in the controllers (setting the value in the hash yourself) and take out attr_accessible.

I realized there is probably no reason to use validations on something that users cannot access. I chose to put the changes inside of a transaction ensuring both objects are accounted for.

Nolgthorn fucked around with this message at 22:53 on Apr 17, 2010

Dessert Rose
May 17, 2004

awoken in control of a lucid deep dream...
I've been out of the RoR loop for a long while. I just deployed a tiny serverside app for an iPad app I wrote, and it was a huge pain to deploy on my Joyent lifetime account. Heroku looks absolutely amazing - I love that deployment is just a git push. But I have one snag...

This app depends on push notifications and is really really latency sensitive. When I want to send a push notification it needs to be sent right away. However, Apple requires that push notification providers hold the connection to the service open as long as possible, rather than opening a new one for every notification.

All the current Rails implementations for the APNs use a queue/cron system to batch notifications up in order to get around this requirement. I ended up writing a daemon that maintains the connection to their servers forever, and communicating with it through a named pipe in the app. That works really well for the environment I'm in. However, I'm on a lifetime shared hosting account with low limits. If this app takes off, I'll be screwed if it hammers my servers too much, and have to move it somewhere else (which is sort of a problem I'd like to have...)

Heroku looks like a great place to migrate to if this thing gets too big. Hell, I'd use Heroku for what I currently use my lifetime account for, which is basically a dev sandbox. But I'm wondering about how I'd implement what I have already on their service. I see that they allow for worker processes at 5c/hr (which adds up fast monthly) but I'm not sure whether this setup would actually work for me. I read their overview documentation, and it seems like it COULD work - I'd just have to communicate with it through the database rather than the named pipe, which would be some work. But I don't know what the gotchas are there.

Has anyone else done something like this on Heroku before? I'm just making sure I have an escape plan if I need it.

NotShadowStar
Sep 20, 2000
If something is that sensitive you probably don't want heroku. Look at EC2 or some other VPS-type hosting. I've never done push notifications but it sounds like you're on the right track to have a daemon running that is a broker between the Apple notification service and your app. Also iPhone OS 4 has a local notification daemon that you can use if you can get away with it in your instance.

e: Actually I'd make the local app daemon translate Apple push requests into normal REST API requests into your app instead of doing some funky things with sockets and pipes.

Dessert Rose
May 17, 2004

awoken in control of a lucid deep dream...
Sorry, push notifications go the other way - from my app to the daemon to Apple (and then to devices). The latency I was referring to is the delay between when someone hits my app with a browser and the push notification appears on their device. A couple seconds is fine, but anything over about ten and it loses most of its value.

So the fifo was the easiest way to do that. I actually just took another Ruby daemon that was already out there but used TCP sockets and ripped the Apple connection code out of there and then wrote the fifo part.

Local notifications won't work for this because the push is definitely generated off-device.

The app is just an app that lets you click a bookmarklet and then you get a popup on your device that you can press to open the page you were viewing on your computer. I made it mainly because I found that I was doing that all the time by hand, since the iPad is really nice to read the web on.

I was mostly just asking to see what the capabilities of the workers were and if they could be expected to live long lives. The architecture looks like they might migrate between servers from time to time.

sorghum
Jul 9, 2001
Have you looked at Urban Airship? They have a simple REST API for Apple push notifications and it's free for up to 250k messages per month.

Adbot
ADBOT LOVES YOU

stack
Nov 28, 2000
What is a better way to default to a value in an erb file?

code:
(some_nested_path(@some_var) unless @some_var.nil?) || not_nested_path
(value.name unless value.nil?) || '...'
I run into this all the time and I should think there is a better way to handle this.

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