Skip to content

Instantly share code, notes, and snippets.

@hernandezalek
Last active August 22, 2016 05:14
Show Gist options
  • Save hernandezalek/fae7621ffaa11e0c7856bb80ccefacd7 to your computer and use it in GitHub Desktop.
Save hernandezalek/fae7621ffaa11e0c7856bb80ccefacd7 to your computer and use it in GitHub Desktop.
class ArticlesController < ApplicationController
before_action :authenticate_enterprise!, except: [:index]
def index
@articles = Article.all
end
def show
@article = Article.find(params[:id])
end
def new
@article = current_enterprise.articles.new
end
def edit
@article = Article.find(params[:id])
end
def create
@article = current_enterprise.articles.new(articles_params)
respond_to do |format|
if @article.save
format.html { redirect_to @article, notice: 'article was successfully created.' }
format.js {}
format.json { render json: @article, status: :created, location: @article }
else
format.html { render action: "edit" }
format.json { render json: @article.errors, status: :unprocessable_entity }
end
end
end
def update
@article = Article.find(params[:id])
respond_to do |format|
if @article.update(articles_params)
format.html { redirect_to @article, notice: 'article was successfully created.' }
format.js {}
format.json { render json: @article, status: :updated, location: @article }
else
format.html { render action: "edit" }
format.json { render json: @article.errors, status: :unprocessable_entity }
end
end
end
def destroy
@article = Article.find(params[:id])
@article.destroy
redirect_to articles_path
end
private
def articles_params
params.require(:article).permit(:title, :subtitle, :picture, :content)
end
end
<% if @article.errors.any? %>
Materialize.toast('Ocurrio un error!', 4000)
<% else %>
Materialize.toast('Se creo correctamente!', 4000);
$('#modal1').closeModal();
$(".col").append("<%= escape_javascript(render partial: 'list', locals: {article:article}) %>");
<% end %>
$('#modal1').append('<%= j render "form" %>').openModal();
<!-- Modal Structure -->
<div class="modal-content">
<h2>Articulo</h2>
<%= form_for @article, :html => { :multipart => true }, :remote => true, authenticity_token: true do |f| %>
<% if @article.errors.any? %>
<div id="error_explanation">
<h2>
<%= pluralize(@article.errors.count, "error") %> prohibited
this article from being saved:
</h2>
<ul>
<% @article.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="input-field col s12 m12 l12">
<%= f.text_field :title, autofocus: true, placeholder: 'Titulo' %>
</div>
<div class="input-field col s12 m12 l12">
<%= f.text_field :subtitle, placeholder: 'Subtitulo' %>
</div>
<div class="input-field col s12 m12 l12">
<div class="file-field input-field">
<div class="btn teal accent-5">
<span>IMG</span>
<%= f.file_field :picture %>
</div>
<div class="file-path-wrapper">
<input class="file-path validate" type="text" placeholder="Agregar imagen principal del articulo">
</div>
</div> <br>
</div>
<div class="input-field col s12 m12 l11">
<%= f.text_area :content, class: 'materialize-textarea', placeholder: 'Contenido' %>
</div>
<div class="row">
<div class="input-field col s12 m12 l12 center">
<%= f.submit "Crear articulo", class: 'btn teal accent-5' %>
</div>
</div>
<% end %>
</div>
<div class="modal-footer">
<a href="#!" class="modal-action modal-close waves-effect waves-green btn-flat">Cancelar</a>
</div>
<% if @articles.any? %>
<% @articles.each do |article| %>
<%= render partial: "list", locals: {article:article } %>
<% end %>
<% else %>
<p class="center">No hay articulos disponibles por el momento</p>
<% end %>
<div class="col s12 m6 l4">
<%= link_to article_path(article) do %>
<div class="card z-depth-1">
<div class="card-image valign-wrapper" style="background-image: url( <%= article.picture.url %> );">
<h5 class="white-text block-center"><b><%= article.title %></b></h5>
</div>
<div class="card-action center">
<% if enterprise_signed_in? %>
<%= link_to article_path(article), method: :delete, data: { confirm: '¿Estas seguro de querer eliminar este articulo?' }, class: "btn-floating waves-effect waves-light red" do %>
<i class="material-icons">delete</i>
<% end %>
<%= link_to edit_article_path(article), remote: true, class: "btn-floating waves-effect waves-light teal" do %>
<i class="material-icons">edit</i>
<% end %>
<% end %>
<a class="btn-floating waves-effect waves-light red"><i class="material-icons">favorite</i></a>
</div>
</div>
<% end %>
</div>
$('#modal1').append('<%= j render "form" %>').openModal();
<% if @article.errors.any? %>
Materialize.toast('Ocurrio un error!', 4000)
<% else %>
Materialize.toast('Se creo correctamente!', 4000);
$('#modal1').closeModal();
$(".col").append("<%= escape_javascript(render partial: 'list', locals: {article:article}) %>");
<% end %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment