Skip to main content

Simple newsletter sign up?

Rails • Asked by Gareth Gomersall

Hey Chris and all other members of this forum....

I'm trying to create a single page rails site, for a new app that i'm trying to build myself (it's called Nibli, and it's on nibliapp.com).

I've been scouting the net everywhere to get some information on building a newsletter signup form on the home page i.e. to store data in a database that can be accessed by me later with details like - name, email, device (i.e. iPhone or Android) and country... However i have no clue where to start in building this controller and model for the newsletter (which is what i'm assuming i would need to do).

I don't want it to link to mail chimp, i'd like it to have a local database on the server from which i can get this info.... for later use.

Cheers for any help in advance
Gareth


Great little project. Maybe I can record a screencast for this over the weekend. The gist of it is going to be really simple:

  1. You'll have a model called Lead or something like that. It will have columns for the attributes you want to store: email, name, device, etc.
  2. You'll set up a LeadsController that can have the create action to save the Lead and renders a response.
  3. On the action that displays the homepage (I'll just call this LeadsController#index for now), you would create a <%= form_for @lead, remote: true do |f| %> and in the controller, you would set @lead = Lead.new.
  4. An optional piece here is the remote: true option. This will set up the object, the form, and configure it to be sent via Javascript to the server. The server would return some Javascript back to update the page. One example would be to remove the form after it's submitted and replace it with a "Thank you!" message. Since you made a form for a new Lead, this form will send to the create action. You'd put your javascript in a create.js.erb view file if you went with the remote true option.
  5. That's pretty much it. You will probably want to store a cookie after create so that the next time the user visits the site again, they don't see the lead form twice.

You'll also need something to auto-detect the device probably unless you want the user to select it in the form. Something like this Browser gem might be helpful.

It's really not too hard, but it can be a lot to wrap your head around. This would make for a great screencast though, so I'll be sure to record a version of how to do this soon.

What parts here seem the most daunting?


Hey Chris....

Awesome that you replied to this so quick :)

To be honest, everything about this seems daunting haha. I've done the one month rails course, but i still seem to hit a brick wall and go completely blank when ever i'm trying to get into a new app in rails... It's the same for another app that i'm working on (which is purely web based).

For the device option, I think i would prefer like a dropdown with the two devices listed that the user can select (same as for the country i.e. dropdown with all countries listed) .

A screen cast for this would be wicked! i've got the html and css for this page all done, but it's just this little piece of work that's kicking my ass!

Cheers again for the quick reply and the offer of a screen cast :)
Gareth


Alright, I got an episode recorded for you! http://videos.gorails.com/medias/4h3x4wsnhp

Give it a watch and see what you think. I'm curious how you feel about the speed of the episode. I touched on a lot of things all at once but tried to break them down just enough. Hopefully you'll find it useful!

I think I'll try releasing this out as a full GoRails episode as well and see what people think.


Hey Chris... I've watched the video and it's awesome, just what i was looking for... and couldn't see any where online to do it.

I'm going to watch the video again and then put it into practice.

Thanks again for creating this video, you're a legend!

Cheers
Gareth


Nice video, glad Gareth got some help. I like the idea of storing the cookie to hide the form. Not foolproof but definitely better than being prompted to sign up again. Awesome!


Hey Chris,

So i went through the video over the weekend, and i thought i had got it sussed out... however i find that when i submit my form nothing saves in the database.... locally or on my heroku test site....

It seems as though when i submit the form, i get a long link in the address bar i.e.

https://nibliapp.herokuapp.com/?utf8=%E2%9C%93&authenticity_token=%2B0V7rJ2zPOzliXW4F1FCYjxT8diw4R07VDOavzWfzWc%3D&lead%5Bname%5D=Gareth&lead%5Bemail%5D=ggomersall%40gmail.com&lead%5Bdevice%5D=Android&lead%5Bcountry%5D=AF

this happens locally as well

Perhaps you can give a bit of feed back see links below, any would be much appreciated (but not limited to Chris, I know there are some other very switched on guys on this forum :) )

Github:
https://github.com/ggomersall/Nibli_leads

Heroku app:
https://nibliapp.herokuapp.com

I've installed activeadmin without devise, you can check out the leads on there as well:
https://nibliapp.herokuapp.com/admin/leads

Again any help would be much appreciated...

Cheers
Gareth


That would happen from missing the method: "POST" parameter in your $.ajax call. When a form is submitted via GET, it sends all the fields over in the URL instead of in the body.

If you make sure that it's sending a POST through, then you should be set.


ok i'll watch it again and see what's cracking! :)


Off topic sort of Gareth. It looks like your assets aren't being served up on your pages index.html.erb. Good practice is to put your images into appname/app/assets/images' then simply serve them in your view as<%= image_tag 'yourimage.png' %>. Right now it looks like you're trying to serve them out of/public` but I think your syntax in the view might be incorrect. Just an observation. Hope you get your site worked out.


Thanks for the headsup re the images James...

Chris,
I've figured it out... my button for submit, was just ----
div class="form-group-lg">
div class="col-md-offset-4 col-md-4 formspace">
button type="submit" class="btn btn-primary btn-lg">Submit
/div>
/div>

instead of -----
div class="form-group-lg">
div class="col-md-offset-4 col-md-4 formspace">
%= f.submit 'Submit'%>
/div>
/div>

It's amazing what how such a simple thing can ruin everything else haha

Cheers for the video again....

Gareth


Oh awesome, glad you got it working! :)


Actually chris... that wasn't the issue.. urgh! but i did figure out what was the problem....

the - <%= form_for @lead do |f| %> needed to be outside of the form divs i.e. before (form class="form-horizontal center")

does that make sense... anyway i've got it working now.... phew!!! So enough harassment from me, for now haha

Cheers
Gareth


Yup, looks like you are saving to database now. Glad you got it working. I just signed up myself on your app to test. ActiveAdmin shows my lead.

Your assets are still an issue so I'm not sure if you re-deployed with the proper code or not. If you get stuck, let me know.


Thanks James, i got it sorted, i misspelt the names of the images... DOH!


Looking good! Glad you got everything figured out.


It's all down to you and James :) so thanks a bunch...

I'm just stuck with two things now, getting it on digital ocean, which I'm trying to figure out through your deploy guide, and why my Nibli logo isn't playing nicely and being responsive... Grrr :)


Getting it on Digital Ocean is easy. I can walk you through it. The GoRails deploy guide should give you everything you need to get started. Let us know where you're stuck and we'll chime in to get you all setup.


Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 27,623+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.