Created
October 14, 2012 23:45
-
-
Save xenodesystems/3890182 to your computer and use it in GitHub Desktop.
Xenode Rails Samples #1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def destroy | |
@post = Post.find(params[:id]) | |
@post.destroy | |
redirect_to posts_path, :notice => "Tu post se ha Borrado" | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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 %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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