Skip to content

Instantly share code, notes, and snippets.

@thebucknerlife
Forked from runchal/README.md
Created April 7, 2013 00:44
Show Gist options
  • Save thebucknerlife/5328325 to your computer and use it in GitHub Desktop.
Save thebucknerlife/5328325 to your computer and use it in GitHub Desktop.

N.B. Things that are starred with * are local variable

  • Ok, first generate a new rails app using this format:

    • rails new app_name
    • rails new amit_test_blog
  • Ok, next sketch out your models.

    • We're going to have two models for this blog: User and Post
    • One users can have many posts and many posts can belong to one users
    • Figure out the fields in each model.
    • Users will have these: field:type
      • username:string
      • password:string
      • first_name:string
      • last_name:string
      • email:string
    • Posts will have these: field:type
      • title:text
      • subtitle:text
      • body:text
      • user_id:integer
  • Seriously, that was painful. And it doesn't account for the fact that this only allows for one author! Oh well, let's generate the fucking models. We're going to use this format:

    • rails generate model Modelname field:type
      • Modelname should be singular. You can have multiple field:type thingys. Ok?
    • rails generate model User username:string password:string first_name:string last_name:string email:string
    • rails generate model Post title:text subtitle:text body:text user_id:integer
  • Did everything work out alright for you? Really? Ok. Let's rake some shit to create the databases and shit:

    • rake db:migrate
  • Ok, now we have to set up our fucking routes. I actually don't know if this should be done before or after setting up controllers but fuck it.

    • Open up /config/routes.rb
    • Add, just below the first line, this:
      • resources :users
      • resources :posts
    • Wait, isn't there some shit with get "users/new" and shit? YES but I haven't figured it out yet so hold on, mmkay?
    • Ok, you can run rake routes to see that your routes have been set up and you saved your file. You did save your file, right dummy? Ok it looks like all the shit is setup right. You should see a bunch of GET and PUT and DELETE shit that I still don't understand, but apparently it's like an HTTP call or some shit like that. I dunno. Google it.
  • Maybe it's time to run the server.

    • rails server
  • Ok, if you try to go to your localhost:3000/users file or localhost:3000/posts you won't see shit. Why? Because you don'thave any Controllers. Ugh, this shit is starting to suck.

  • Ok, let's spin up our Controllers. You have to figure out the Name of your controller (which is the same as your model name, BUT PLURAL SERIOUSLY PLURAL) and also the actions which are the things you want your controller to do. So what the actions you want? Well Rails ran a bunch of shit already when we ran rake resources. So let's use index, create, new, edit, show, update, destroy. I don't know if we need all of them, but fuck it. Here's the format:

    • rails generate controller Controllername action
    • rails generate controller Users index create new edit show update destroy
    • rails generate controller Posts index create new edit show update destroy
    • Ok, that seemed to work. And if you look in your config/routes.rb file it did a bunch of shit by putting gets all over the place. And if you look at localhost:3000/users and localhost:3000/posts you'll see a welcome page. Ok, this is getting promising.
    • Psych, it's about to get way fucking worse. Doofus.
  • OK OK let's modify our fucking controllers so they actually do something. Your post controller, for example, is going to look like this:

      class PostsController < ApplicationController
        def index
        end
    
        def create
        end  
    
        def new
        end  
    
        def edit
        end  
    
        def show
        end  
    
        def update
        end  
    
        def destroy
        end
    
      end
    
  • Well shit, that doesn't do anything, does it? Ok, so now you have to start adding in the shit to make this shit actually DO something. Ok, let's fucking spin some shit up, yo. Insert this shit into your code:

Hello. Still here? Good.

	class PostsController < ApplicationController
		def index
			@posts = Post.all
	
			respond_to do |format|
				format.html
			end
		end
	
		def create
			@post = Post.new(params[:post])
	
			respond_to do |format|
				if @post.save
					format.html { redirect_to @post, notice: 'Good job creating your post. You must be some sort of genius.' }
				else
					format.html { render action: "new" }
				end
			end
		end
	
		def new
			@post = Post.new
			
			respond_to do |format|
				format.html
			end
		end
	
		def edit
			@post = Post.find(params[:id])
		end
	
		def show
			@post = Post.find(params[:id])
			
			respond_to do |format|
				format.html
			end
		end
	
		def update
			@post = Post.find(params[:id])
	
			respond_to do |format|
				if post.update_attributes(params[:post])
					format.html { redirect_to @post, notice: 'Your post was successfully updated. Dummy.' }
				else
					format.html { render action: "edit" }
				end
			end
		end
	
		def destroy
			@post = Post.find(params[:id])
			@post.destroy
	
			respond_to do |format|
				format.html {redirect_to posts_url}
			end
		end
	end
  • Holy fucking shit. Seriously, you wouldn't believe how long that took me to get working properly, mostly because I was missing a do in a respond_to function. Ok, let's move on. Um. So if you check your rails app in the browser at localhost:3000/posts all that shows up is some fucking bullshit. That's fucking lame. So let's look at the views folder.

    • Here's what we have:

      • create.html.erb
      • destroy.html.erb
      • edit.html.erb
      • index.html.erb
      • new.html.erb
      • show.html.erb
      • update.html.erb
    • Let's look at the index.html.erb file first. There's some bullshit there that Rails generated. Let's get rid of that shit and have a blank template.

    • What kind of layout makes sense? We've done tables in the past but that's prolly stupid for a blog. So let's just do all the posts, each separated by a horizontal rule. Here's what that code looks like.

    • Also, it's fucking bullshit that Markdown isn't parsing this HTML as code blocks. Sorry. index.html.erb

      These are all the posts ever made

      <% @posts.each do |post|%>

      <%= post.title %>

      <%= post.subtitle %>

      <%= post.body %>

      <%= link_to 'Permalink', post %>


      <% end %>

      <%= link_to 'Write a Post', new_post_path %>

  • Ok, refresh that page. What happens? Nothing. Because you haven't written any posts. Ok, well click on "Write a post." And what happens? NOTHING. Duh. Ok, open up new.html.erb in the posts view.

    • Delete all the crap.

    • Let's create a form! YAYAYAYAY. new.html.erb <%= form_for @post do |form| %>

        <ul>
        	<% @post.errors.full_messages.each do |message| %>
        		<li><%= message %>
        	<% end %>
        </ul>
      
        <%= form.label :title, "What's your title?" %><br>
        <%= form.text_field :title, placeholder:"Why do you deserve to be listened to?" %>
        <br>
        <br>
        <%= form.label :subtitle, "What's your subtitle?" %><br>
        <%= form.text_field :subtitle, placeholder:"Why do you deserve to be listened to?" %>
        <br>
        <br>
        <%= form.label :body, "What's your content?" %><br>
        <%= form.text_area :body, placeholder:"Why do you deserve to be listened to?" %>
        <br>
        <br>
        <%= form.label :user_id, "What's your user id?" %><br>
        <%= form.text_field :user_id, placeholder:"Like, a number?" %>
        <br>
        <br>
        <%= form.submit %>
      

      <% end %>

  • Ok, that's not totally fucking terrible. I mean, yeah it kinda is, but it's not TOTALLY fucking terrible. OK, let's take a fucking break, yo because there's a bunch of shit we haven't done yet.

  • Ok, refreshed? Let's generate the rest of the fucking forms and ERBs and shit and complete the Posts section of the site. Remember, we still have the users section to attack.

  • Here's the HTML for the show.html.erb show.html.erb

    <%= @post.title %>

    <%= @post.subtitle %>

    <%= @post.body %>

    <%= @post.user_id %>

      <%= link_to 'Edit this Post', edit_post_path %>
    
      <%= link_to 'Delete this Post', @post, method: :delete, data: { confirm: 'Are you sure?' } %>
    
      <%= link_to 'Back', posts_path %>
    
  • This allows us to edit the post, delete the post, and go back to see all the posts.

  • Ok, let's create the edit.html.erb edit.html.erb

    Edit this fucking post

      <%= form_for @post do |form| %>
    
      	<ul>
      		<% @post.errors.full_messages.each do |message| %>
      			<li><%= message %>
      		<% end %>
      	</ul>
    
      	<%= form.label :title, "What's your title?" %><br>
      	<%= form.text_field :title, placeholder:"Why do you deserve to be listened to?" %>
      	<br>
      	<br>
      	<%= form.label :subtitle, "What's your subtitle?" %><br>
      	<%= form.text_field :subtitle, placeholder:"Why do you deserve to be listened to?" %>
      	<br>
      	<br>
      	<%= form.label :body, "What's your content?" %><br>
      	<%= form.text_area :body, placeholder:"Why do you deserve to be listened to?" %>
      	<br>
      	<br>
      	<%= form.label :user_id, "What's your user_id?" %><br>
      	<%= form.text_field :user_id, placeholder:"Like, a number?" %>
      	<br>
      	<br>
      	<%= form.submit %>
      <% end %>
    
      <%= link_to 'Show', @post %>
      <%= link_to 'Back', posts_path %>
    
  • Yayayayay. So we've got index.html.erb and new.html.erb and show.html.erb and edit.html.erb. There's some ERBs that we created when we were spinning out the controller like create.html.erb and destroy.html.erb and update.html.erb. I'm pretty sure we can destroy them, but to be honest, I'm not totally sure. The pages don't really do shit so we can prolly get rid of them but until then, let's just leave them there since there's not much to do with this now.

  • And now, my friend — if I may be so bold to address you as such — let us pause for a second, and reflect. There are still some things we should do with this part of the app. Things like:

    • Tying in the User model and controller. Right now we have to manually associate the two, which makes no fucking sense.
    • Maybe doing some partials for the form. It seems like it's being repeated for the new and edit section.
    • Start spinning in nested resources for comments just for practice. Ok. That's a fair amount to do. Let's figure out how to attack this. Now that I kinda have a grasp on this, I'd probably attack the partial first, then the User MVC and then the nested resources last.
    • But I need to take a break and will maybe try to finish it up tomorrow. Here's a cool song to listen to in the meantime.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment