Last active
August 22, 2016 05:14
-
-
Save hernandezalek/fae7621ffaa11e0c7856bb80ccefacd7 to your computer and use it in GitHub Desktop.
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 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 |
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
<% 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 %> |
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
$('#modal1').append('<%= j render "form" %>').openModal(); |
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
<!-- 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> |
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
<% 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 %> |
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
<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> |
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
$('#modal1').append('<%= j render "form" %>').openModal(); |
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
<% 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