Skip to content

Instantly share code, notes, and snippets.

@xenodesystems
Created October 14, 2012 23:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xenodesystems/3890182 to your computer and use it in GitHub Desktop.
Save xenodesystems/3890182 to your computer and use it in GitHub Desktop.
Xenode Rails Samples #1
def destroy
@post = Post.find(params[:id])
@post.destroy
redirect_to posts_path, :notice => "Tu post se ha Borrado"
end
end
def update
@post = Post.find(params[:id])
if @post.update_attributes(params[:post])
redirect_to posts_path, :notice => "Tu post se ha actualizado"
else
render "edit"
end
end
# ActiveRecord CRUD (Create-Read-Update-Destroy)
# CREAR POSTS:
Post.create(:title => "Nuestro Primer Post", :content => "Contenido para el primer post")
# LEER/ENCONTRAR POSTS:
Post.all # (todos)
Post.find(id) # (uno)
# Después de encontrar 1 post:
Post.title # (Nos regresa el título de ese post)
Post.content # (Nos regresa el contenido de ese post)
# ACTUALIZAR POSTS:
post.update_attribute(:content, "Contenido Cambiado")
# (cambia 1 atributo) [con/de la variable "post" definida con los métodos de LEER]
post.update_attributes(:title => "Primer Post", :content => "El contenido")
# (para cambiar todos los atributos) [con/de la variable "post" definida con los métodos de LEER]
# DESTRUIR POSTS:
post.destroy(id) # [con variable "post" que nosotros elijamos según id]
class PostsController < ApplicationController
def index
@posts = Post.all
end
def show
@post = Post.find(params[:id])
end
def new
@post = Post.new
end
def create
@post = Post.new(params[:post])
if @post.save
redirect_to posts_path, :notice => "Tu post se ha salvado"
else
render "new"
end
end
def edit
@post = Post.find(params[:id])
end
def update
@post = Post.find(params[:id])
if @post.update_attributes(params[:post])
redirect_to posts_path, :notice => "Tu post se ha actualizado"
else
render "edit"
end
end
def destroy
@post = Post.find(params[:id])
@post.destroy
redirect_to posts_path, :notice => "Tu post se ha borrado"
end
end
<!DOCTYPE html>
<html>
<head>
<title>Blog</title>
<%= stylesheet_link_tag "application", :media => "all" %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
</head>
<body>
<% flash.each do |key, value| %>
<p> <%= value %> </p>
<% end %>
<%= yield %>
</body>
</html>
class User < ActiveRecord::Base
authenticates_with_sorcery!
attr_accessible :username, :email, :password, :password_confirmation
validates_confirmation_of :password
validates_presence_of :password, :on => :create
validates_presence_of :username
validates_uniqueness_of :username
validates_presence_of :email
validates_uniqueness_of :email
end
class UsersController < ApplicationController
def index
end
def show
end
def new
@user = User.new
end
def create
@user = User.new(params[:user])
if @user.save
redirect_to root_url, :notice => "Signed up!"
else
render :new
end
end
def edit
end
def update
end
def destroy
end
end
<h1>Editar Post</h1>
<%= form_for @post do |f| %>
<% if @post.errors.any? %>
<h2>Errores:</h2>
<ul>
<% @post.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
<% end %>
<p>
<%= f.label :title %><br />
<%= f.text_field :title %><br />
<br />
<%= f.label :content %><br />
<%= f.text_area :content %>
</p>
<p>
<%= f.submit "Actualizar Post" %>
</p>
<% end %>
<h1> Nuestro Blog </h1>
<% @posts.each do |post| %>
<h2><%= link_to post.title, post %></h2>
<p> <%= post.content %> </p>
<p> <%= link_to "Editar", edit_post_path(post) %> |
<%= link_to "Borrar", post, :confirm => "Estas seguro?", :method => :delete %>
</p>
<hr />
<% end %>
<p><%= link_to "Agregar nuevo post", new_post_path %></p>
<h1>Nuevo post</h1>
<%= form_for @post do |f| %>
<% if @post.errors.any? %>
<h2>Errores:</h2>
<ul>
<% @post.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
<% end %>
<p>
<%= f.label :title %><br />
<%= f.text_field :title %><br />
<br />
<%= f.label :content %><br />
<%= f.text_area :content %>
</p>
<p>
<%= f.submit "Agregar Nuevo Post" %>
</p>
<% end %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment