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
Sub Par
Jul 18, 2001


Dinosaur Gum
I have an idea for an app I'd like to build, and from what I understand, Rails would be a good framework for it and I've been wanting to learn what all this Rails excitement is about, so I'm going to give it a shot. I have some crappy hosting with MySQL and Rails, but all I see in this thread is stuff about Heroku. So I went to look at pricing... but I don't know jack about what a "dyno" or a "worker" is, how much I might use, or whatever.

This is a simple little app (dinky CRM type thing) for me to gently caress around with and learn rails and possibly get something accomplished. It will just be me using it, low traffic and stuff. The lowest settings on the Heroku pricing page give me $36/month which I don't really want to shell out considering this is just for fun. Should I just install the Rails package on my crappy hosting I've already paid for and go that way, or is that Heroku estimate high for what I'd actually be using? Any suggestions? Thanks.

Edit: after re-reading with my comprehension hat on, it looks like I don't need any Workers and it will be pretty much free for me, since Dyno #1 is free. And I don't really care about performance right now. Sorry for the dumb question.

Sub Par fucked around with this message at 23:11 on Jan 7, 2011

Adbot
ADBOT LOVES YOU

Sub Par
Jul 18, 2001


Dinosaur Gum
Ok, so I'm on Windows (Vista) on my laptop, and everything I read says that it is best to develop on Linux. So I've read about using Linux via Virtual PC 2007, and gotten that working, but I can't seem to get Ubuntu 10.10 to install all the way - it hangs halfway through. It's starting to piss me off, so now I'm about to try Gentoo, but is it really a bad idea to develop on Windows? Is all the trouble I'm going through to get this going gonna be worth it?

Sub Par
Jul 18, 2001


Dinosaur Gum
Thanks guys for all your help. After trying repeatedly to install Ubuntu using Virtual PC and failing, I just installed gVim, Git, and the whole bit on Windows. While slow, it seems to be working and I'm up and running on Heroku. Despite all the trouble... this is awesome! Like, two commands and I'm deployed? Love it.

Sub Par
Jul 18, 2001


Dinosaur Gum

rugbert posted:

You mean Virtual BOX right? I thought installation was a breeze, what kind of problems did you have?


Also - Can I change my project name? I want to reuse this project for a couple different sites so I should probably change the project name.

No, Virtual PC (a Microsoft product). Installation just stopped when the progress bar was about 60% complete. I don't know why. I tried it several times, and even let it sit for several hours just in case it wasn't stopped but just taking a while. No dice.

Things seem to work well in Windows. Installed gVim, Git, etc and am working through one of the tutorials posted upthread just fine and dandy.

Sub Par
Jul 18, 2001


Dinosaur Gum
I'm a newb at Rails stuff but I've followed the Rails Tutorial that was recommended here, and now I have a small app. I am playing around with the Facebook API using Omniauth-Facebook and Koala, which is going well. But I want to to set up something like this:

1) User logs in to my site using their email and password, gets a session with a token that's stored in the user table and is a random base64 string (SecureRandom.urlsafe_base64) - this is done and works fine.

2) I have an app on Facebook. User clicks a button on my site once logged in to authorize the app. This is handled via OmniAuth. This is done-ish.

3) Facebook generates a token which I persist and then I generate session/cookie that tracks them

What I'm having a hard time with is #3 - I'm generally following this for the Omniauth stuff, but I don't know enough about how sessions work to know if I can simply create a second session for the same user or if I have to create a different controller for the Facebook session type? Basically I want to be sure that my app knows which session to look at when doing stuff on my site (logging in, logging out, changing their contact info) versus doing stuff on my site via Facebook (using the FB session and token). Hopefully this makes sense?

Sub Par
Jul 18, 2001


Dinosaur Gum
Excellent, thank you, I think that puts me on the right track.

Sub Par
Jul 18, 2001


Dinosaur Gum
Ok so now I'm trying to troubleshoot an error. I can't reproduce it in development, the thing works just fine there. But when I deploy to Heroku and Facebook hits /auth/facebook/callback, I get a 500 - internal server error.

I can't figure out why that would be happening. Heroku logs says:

code:
2013-03-22T20:21:49+00:00 heroku[router]: at=info method=GET path=/auth/facebook
/callback host=shrouded-woodland-XXXX.herokuapp.com fwd="98.195.155.246" dyno=we
b.1 queue=0 wait=0ms connect=2ms service=27ms status=500 bytes=643
Which isn't super helpful to me. Is there some way to get a more verbose error?

Sub Par
Jul 18, 2001


Dinosaur Gum
Yeah after an hour of failing I just paid the $12 for Exceptional and got my back trace in seconds. A++ would buy again.

Sub Par
Jul 18, 2001


Dinosaur Gum
I'm looking to store a person's username in the database (Postgres - Heroku) as entered but add a case-insensitive unique index on the username column. So for example, I want a user to be able to sign up as "MyUserName" and have my application display the username as entered, while still disallowing other users from signing up as "myusername".

The only solution I can find on google is to downcase/upcase the username prior to shoving it into the DB since Postgres' unique indexes are case sensitive by default. This accomplishes uniqueness, but doesn't allow me to display the username as entered. Were I operating in the database, I would just
code:
CREATE UNIQUE INDEX IX_Unique_Username ON users (lower(username))
But it seems to me there should be some way to just do this in my migration? If that's not possible I guess I can just have a second column called display_username or something but that seems kind of dumb.

Sub Par
Jul 18, 2001


Dinosaur Gum
Either seems like a kludge but I guess the kludge that makes the app more db-agnostic and allows users to potentially change their display name if I decide on that later will win. Thanks for that link though, it's helpful to see how to execute statements like that as part of a migration.

Sub Par
Jul 18, 2001


Dinosaur Gum

Pardot posted:

I am very interested in understanding why because you're not operating the database you think you can't do this. Is there something we could have improved so you would have just done this right off?

I'm just very new to rails but 95% of my day job involves writing SQL. As a result, I'm very familiar and comfortable doing things directly in the database. I would like to just be able to (and maybe I can and just don't know?) do
code:
heroku run postgres console
create index blah blah;
exit
I didn't realize until yesterday that I could execute raw SQL in the manner described as part of a migration so that does solve my problem. As for what could be changed, I guess the change I want is actually with Postgres, not rails. In SQL Server you can specify whether your collation is case-sensitive, and I would like to be able to change that setting (if it even exists with PG) when deployed to Heroku, which I don't believe I can. Then I could do that in some config file and in my migration just create a regular old unique index and be done with it.

And to the other poster who suggested having my users log in with email address, that's what they do. But there are public-facing aspects to the app that need to be attached to a name, and I don't want them to have to expose their email address in that way, thus the username. And it would drive me insane if I signed up as "Sub Par" and the app listed me as "SUB PAR" or "sub par".

Sub Par fucked around with this message at 15:49 on Mar 28, 2013

Sub Par
Jul 18, 2001


Dinosaur Gum
Yeah I've done (limited) work with Postgres in the past and I've been impressed. I'm excited for Rails 4 to directly expose some of the other datatypes. Anyway thanks for all that, it will surely come in handy!

Sub Par
Jul 18, 2001


Dinosaur Gum
My app has a "feed" of sorts where a user logs in and a bunch of stuff is displayed similar to a Facebook feed. Each item on the feed has a static URL associated with it, and that URL is obviously available in the view. I would like a user to be able to bookmark that static URL by clicking a link. I have a bookmark model that belongs_to user and a user model that has_many bookmarks. But I can't wrap my head around how to make a single URL that the user can click that will create an entry in the bookmark table. Does this make sense?

Sub Par
Jul 18, 2001


Dinosaur Gum
I have a form on which you can post an item, and when you do, I want to handle that post with AJAX and re-render the list of posts. I have my list of posts set up like this:
code:
<div class = "posts" id="feed_entity_<%= f.entity_id %>">
	<%= render @posts.where(:entity_id => f.entity_id) %>
</div>
In create.js.erb though I'm not sure what to enter. My most recent try was this:
code:
$("#feed_entity_<%= params[:post][:entity_id] %>").html("<%= escape_javascript(render partial: 'post') %>");
I've verified that the params are correct and that the right div is being identified by doing .html("foobar") and seeing the correct area of the page replaced with the text "foobar". It's the render part that's tripping me up. I usually have my partials in /shared and I just use basically the same code in html() that I do in the actual .html.erb file. But the construction (which admittedly I copy/pasted from the Hartl tutorial) "render @posts" leaves me not knowing what to do.

Sub Par
Jul 18, 2001


Dinosaur Gum

Sub Par posted:

I have a form on which you can post an item, and when you do, I want to handle that post with AJAX and re-render the list of posts. I have my list of posts set up like this:
code:
<div class = "posts" id="feed_entity_<%= f.entity_id %>">
	<%= render @posts.where(:entity_id => f.entity_id) %>
</div>
In create.js.erb though I'm not sure what to enter. My most recent try was this:
code:
$("#feed_entity_<%= params[:post][:entity_id] %>").html("<%= escape_javascript(render partial: 'post') %>");
I've verified that the params are correct and that the right div is being identified by doing .html("foobar") and seeing the correct area of the page replaced with the text "foobar". It's the render part that's tripping me up. I usually have my partials in /shared and I just use basically the same code in html() that I do in the actual .html.erb file. But the construction (which admittedly I copy/pasted from the Hartl tutorial) "render @posts" leaves me not knowing what to do.

I fixed this when I realized that the @posts instance variable belonged to the User controller and wasn't getting into the Posts controller to be passed to re-render the partial. Added @posts to the Posts controller create method and everything fell into place.

Sub Par
Jul 18, 2001


Dinosaur Gum
I'm building a simple search form and trying to determine if this approach is safe from an SQL injection point of view:
code:
@users = User.where("username like ?","%#{params[:search]}%")
I can't seem to find an answer that's on point. Is there a better/safer way to accomplish this?

Sub Par
Jul 18, 2001


Dinosaur Gum

kayakyakr posted:

That should be ok. The cleaner will clean the full string out. If you get anything more complex than that, you should really look into integrating sphinx.

Thanks. I plan to, once I can spend some money.

Sub Par
Jul 18, 2001


Dinosaur Gum

kayakyakr posted:

what are you hosting on? we successfully ran sphinx + thinking_sphinx (rails gem) side-by-side with unicorn on a single virtual server.

I have a proof of concept type thing up on Heroku and am just trying to drum up interest in the idea. If I can get my friends on board and put up a small amount of cash I am going to go with a VPS, but Heroku for now. Not worth even the $12/mo for the sphinx-based add-ons right now for my ~600 users.

Sub Par
Jul 18, 2001


Dinosaur Gum
Sweet, I'll look into that. Thanks.

I'm intrigued by the pricing of DigitalOcean and Linode. Anyone have a guide handy that can help walk me through migrating from Heroku to either of these? I'm starting to get frustrated with the add-on situation (add-on for email, monthly fees for SSL, poo poo like that). Thanks.

Sub Par
Jul 18, 2001


Dinosaur Gum
This isn't necessarily a rails question but I have a rails app on Heroku with the free tier postgres database. I have a table that is going to be populated by several RSS feeds - I consume the feeds once a day and add new URLs to this table. The url field is a string(255) and I have it indexed for uniqueness. Here's my question:

My application code does this check before inserting new items:
code:
if Feeditem.where(:url => incoming_url) == []
    <insert item>
end
This feels awkward and slow/lovely when there will be thousands of rows in there. Is there a better way to do this that I'm not thinking of? Should I hash the URL and store the hash in the same table and use the hash in the where instead?

Sub Par
Jul 18, 2001


Dinosaur Gum
Thanks guys. The check is in the application code (instead of just rescuing failed inserts) because prior to inserting a record I am parsing the Open Graph metadata from the URL and storing it as well, and I don't want to do that over and over again because it is slow. I have more appropriate checks on uniqueness elsewhere, this is mostly to prevent parsing OG metadata for URLs the application has already seen.

Sub Par
Jul 18, 2001


Dinosaur Gum
Since I'm in here I may as well ask, does anyone have a preferred method/gem for fixing mixed content warnings? The app is SSL but some of the Open Graph images I'm displaying are http:// and so I'm getting mixed content warnings. I'm pretty close to writing my own image proxy because everything I can find looks too complicated but if there's another way that would be great!

Sub Par
Jul 18, 2001


Dinosaur Gum

Smol posted:

But doesn't Open Graph supports TLS just fine? :confused:

I may be too dumb to know what this means. I am fetching the open graph metadata from various webpages and displaying the image specified by og:image in the application. Most of the time, this is an image like http://my-insecure-server.com/someimage.jpg and I need a way to prevent the browser from identifying this as "mixed content", so proxying it through my server like Facebook does or through some other means.

Sub Par
Jul 18, 2001


Dinosaur Gum
Thanks a bunch, that seems to have resolved things.

Sub Par
Jul 18, 2001


Dinosaur Gum
Another dumb question from me! I have Post model (in a Rails 3 app) that has just a couple attributes: user_id, title, content. I have the model set up to protect against mass assignment so people can't post as other users
code:
attr_accessible :title, :content
I would like to set up a system that automatically posts system messages using an account named "ServiceAccount" or whatever with user_id of 10. In the controller, I'm building these posts like this:
code:
Post.create(:title => some_object.title, :content => some_object.title, :user_id => 10)
This obviously doesn't work. I do not want to add :user_id to attr_accessible. Is there some way to accomplish this without exposing Post model to security risks?

Sub Par
Jul 18, 2001


Dinosaur Gum
Edit: ok I did this which seems to do what I want and keep things secure. In the model:
code:
attr_accessible :title, :content
attr_accessible :title, :content, :user_id, as: :system_post
In the method:
code:
@p = Post.new({title: some_object.title, content: some_object.content, user_id: 10}, as: :system_post)
if @p.save
   <etc>

Sub Par fucked around with this message at 17:41 on May 22, 2014

Sub Par
Jul 18, 2001


Dinosaur Gum

prom candy posted:

Depending on how big this app is it might be a good idea to just upgrade to Rails 4 and use strong_params for this. It's what it was built for pretty much.

Yeah that's what I wanted to do. It's not a huge app but it's something I was working on months ago while trying to learn rails, and I've just recently come back to it and I don't feel like going through all that. At this stage I have about 50 users and it's proof-of-concept level complete. Once I make it feature-complete I will probably upgrade to rails 4.

Thanks for the suggestions everyone. I'm going to try out a few different approaches and see which I like best and then just move on. Since I know this can be done more easily in rails 4 I may just ditch this feature for now and come back to it after the upgrade.

Sub Par
Jul 18, 2001


Dinosaur Gum
I have some outbound links on my rails app (Rails 4, running on Heroku). When they are clicked, they are tracked in my database. One of the fields I'm hoping to capture about each click event is the IP address of the person clicking. I am having a devil of a time doing this. I'm currently doing this here thing:
code:
@click.request_ip = request.env["X_FORWARDED_FOR"]
Which works some times but not others, and I can't figure out why. The consensus on stackoverflow seems to be "doing this is hard when you're hosted on Heroku and oh by the by people can spoof IPs", but I don't care about spoofing and Heroku's docs seem to indicate that I'm doing it right. Am I?

Sub Par
Jul 18, 2001


Dinosaur Gum

Smol posted:

X-forwarded-for is added by reverse proxies (e.g. nginx in front of your rails app). If you're not seeing it in development, then that's probably the reason. Anyhow, ActionContoller::Request#remote_ip is what you're looking for.

This is what I originally had, but because of the way Heroku works, I was getting nothing but 10.xx.xx.xx IPs that were likely some kind of Heroku load-balancing or whatever. Anyway I'm now trying this:
code:
@click.request_ip = request.env['HTTP_X_REAL_IP'] ||= request.env['REMOTE_ADDR']
We'll see how that goes.

Sub Par
Jul 18, 2001


Dinosaur Gum
I have a table that tracks which users have access to which resources, so it's just user_id and resource_id. I want to introduce a function where an admin can give 5 random users access to a given resource. I'm trying to think through the most efficient way to find 5 users that don't already have access to the resource. I know exactly how I'd write this in SQL but I can't think of a way to make this "rails-y". I would do:

code:
select top 5 id
from Users
where not exists (
     select 'X'
     from UserResources
     where UserResources.user_id = Users.id
          and UserResources.resource_id = 1)
order by newid()
I am going to just do this with raw SQL for now but I'm wondering (hoping?) there's a concise way to do this that i just don't know?

Sub Par
Jul 18, 2001


Dinosaur Gum
Excellent, thanks. I'll play around with this.

Sub Par
Jul 18, 2001


Dinosaur Gum
I am a hobbyist trying to improve on a small web app I have. It's a super simple app that queries an external API which pulls a bunch of data into my app's database. The app then does some math and displays output to the user. It's a rails 5 app running on Heroku.

I'm currently just letting the web dyno on Heroku do all this work. The call to the API takes ~20 seconds for most users (the math part is trivial, less than 500ms) so the web dyno doesn't time out. But I'm starting to get more users, and obviously the web dyno doing all this work for user A prevents user B from accessing the site until the work is done. Oh, and it's non-optimal that user A is just left hanging for 20 seconds. I'm looking into a way to pass the external API call off to a worker dyno and show the user a page with a spinner that just says something dumb like "hang on a sec", which, when the process is done, redirects the user to the results page.

I've watched the railscasts on Beanstalkd, Resque, and Delayed Jobs, and I'm a little confused about which is the best approach for me. Delayed Jobs seems the simplest and my use case is certainly easy, I just don't understand how to actually asynchronously process poo poo in the background while having the user's browser poll or something to determine when the job is actually complete and take them to the right place. I don't want them to have to F5 to figure out when the job is done, I just want it to process async, and then redirect when done.

Does anyone have any tips/suggestions? Thanks.

Sub Par
Jul 18, 2001


Dinosaur Gum

necrotic posted:

You cannot just "process async" in Ruby, you need something like Delayed Job to handle things in another process. Rails 5 has ActiveJob which would be the quickest way forward, but you still have to run a background process to handle the queue.

I get that, I just can't figure out how to make what I want happen in my controller. I'll post some code when I get home, hopefully that will be more clear.

Sub Par
Jul 18, 2001


Dinosaur Gum
This is exactly what I'm looking for, thanks. I think I can piece together what I need between this and Google. Thanks again.

Sub Par
Jul 18, 2001


Dinosaur Gum
Thanks everyone, I finally got this working. I ended up using Delayed Jobs. I wrote a custom job, then built a table to store information about job state, used the hooks provided by delayed jobs to update that table as job goes from queued -> running -> done, and then used JS polling to check that status table for job completion, at which point I redirect the user to the right place. It's all working magically. Now let's hope I don't bankrupt myself by accidentally spinning up too many dynos on Heroku.

Adbot
ADBOT LOVES YOU

Sub Par
Jul 18, 2001


Dinosaur Gum
I'm trying to do something that I feel should be easy but I'm clearly loving something basic up. I have a rails 7 app and I am trying to use a node package called TipTap in it, using these installation instructions.

I have run:
code:
npm install @tiptap/core @tiptap/pm @tiptap/starter-kit
And I've added
code:
import { Editor } from '@tiptap/core';
import StarterKit from '@tiptap/starter-kit';
At the top of the relevant stimulus controller. I've also precompiled my assets. But I have this annoying poo poo in the console:
code:
Failed to register controller: tiptap (controllers/tiptap_controller) TypeError: Failed to resolve module specifier "@tiptap/core". 
Relative references must start with either "/", "./", or "../".
If I load the js from TipTap's CDN it works, but I'd rather not do that. What am I forgetting to do? I'm for sure relying on too much Rails magic here without understanding the concepts at play (importmap, probably), but I can't find a good explanation of how to do this kind of thing. Every tutorial I read online is like "it's magic and you just type these things and it works", but it isn't working.

Edit: Motherfucker, just
code:
./bin/importmap pin @tiptap/core 
./bin/importmap pin @tiptap/starter-kit
and things work. At least I was right that I just didn't know enough about the Rails magic and importmaps.

Sub Par fucked around with this message at 18:07 on Jan 6, 2024

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