Skip to content

Instantly share code, notes, and snippets.

@xenodesystems
Created October 15, 2012 03:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xenodesystems/3890658 to your computer and use it in GitHub Desktop.
Save xenodesystems/3890658 to your computer and use it in GitHub Desktop.
# [Mongoid] "ActiveRecord like" CRUD (Create-Read-Update-Destroy)
# CREAR POSTS:
Post.create(title: "Nuestro Primer Post", content: "Contenido para el primer post")
# LEER/ENCONTRAR POSTS:
Post.all.to_a # (todos)
Post.all[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.all[id].destroy # [con variable "post" que nosotros elijamos según id]
Post.destroy_all # (Destruir todos)
<!DOCTYPE html>
<html>
<head>
<title>Auth Example</title>
<%= stylesheet_link_tag "application" %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
</head>
<body>
<div id="container">
<div class="user_nav">
<% if current_user %>
Logged in as <%= current_user.email %>.
<%= link_to "Log out", logout_path %>
<% else %>
<%= link_to "Sign up", signup_path %> or
<%= link_to "log in", login_path %>.
<% end %>
</div>
<% flash.each do |name, msg| %>
<%= content_tag :div, msg, :id => "flash_#{name}" %>
<% end %>
<%= yield %>
</div>
</body>
</html>
class SessionsController < ApplicationController
def new
end
def create
user = login(params[:username], params[:password], params[:remember_me])
if user
redirect_back_or_to root_url, :notice => "Logged in!"
else
flash.now.alert = "Email or password was invalid"
render :new
end
end
def destroy
logout
redirect_to root_url, :notice => "Logged out!"
end
end
<%= form_tag sessions_path do %>
<div class="field">
<%= label_tag :username %>
<%= text_field_tag :username, params[:username] %>
</div>
<div class="field">
<%= label_tag :password %>
<%= password_field_tag :password %>
</div>
<div class="field">
<%= check_box_tag :remember_me, 1, params[:remember_me] %>
<%= label_tag :remember_me %>
</div>
<div class="actions"><%= submit_tag "Log in" %></div>
<% end %>
<h1>Lorem ipsum</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<p><%= link_to "A Page Protected with Login", productos_path %></p>
<%= form_for @user do |f| %>
<% if @user.errors.any? %>
<div class="error_messages">
<h2>Form is invalid</h2>
<ul>
<% for message in @user.errors.full_messages %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :username %>
<%= f.text_field :username %>
</div>
<div class="field">
<%= f.label :email %>
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :password %>
<%= f.password_field :password %>
</div>
<div class="field">
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation %>
</div>
<div class="actions"><%= f.submit %></div>
<% end %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment