Created
November 7, 2016 17:15
-
-
Save rafa-acioly/b320bac7614cf80d0e3f888fe62b5ee0 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 RecipeController < ApplicationController | |
before_action :find_recipe, only: [:show, :update, :edit, :destroy] | |
def index | |
@recipes = Recipe.all | |
end | |
def new | |
@recipe = Recipe.new | |
end | |
def show | |
end | |
def edit | |
end | |
def update | |
if @recipe.update(recipes_params) | |
redirect_to @recipe, notice: 'Receita atualizada com sucesso!' | |
end | |
end | |
def create | |
@recipe = Recipe.new(recipes_params) | |
if @recipe.save | |
redirect_to @recipe | |
end | |
end | |
def destroy | |
if @recipe.destroy | |
redirect_to recipes_url | |
end | |
end | |
private | |
def find_recipe | |
@recipe = Recipe.find(params[:id]) | |
end | |
def recipes_params | |
params.require(:recipe).permit(:name, :stuff, :calories, :prepare_mod, :cost, :kind, :portion, :duration, :poster) | |
end | |
end | |
# A view | |
<%= form_for @recipe do |f| %> | |
<p> | |
<%= f.label :name %> | |
<%= f.text_field :name, class:'form-control' %> | |
</p> | |
<p> | |
<%= f.label :stuff %> | |
<%= f.text_area :stuff, cols: 20, rows: 5, class: 'form-control' %> | |
</p> | |
<p> | |
<%= f.label :calories %> | |
<%= f.number_field :calories %> | |
</p> | |
<p> | |
<%= f.label :prepare_mod %> | |
<%= f.text_area :prepare_mod, cols: 20, rows: 5 %> | |
</p> | |
<p> | |
<%= f.label :cost %> | |
<%= f.number_field :cost %> | |
</p> | |
<p> | |
<%= f.label :kind %> | |
<%= f.text_field :kind %> | |
</p> | |
<p> | |
<%= f.label :portion %> | |
<%= f.text_field :portion %> | |
</p> | |
<p> | |
<%= f.label :duration %> | |
<%= f.time_select :duration %> | |
</p> | |
<p> | |
<%= f.label :poster %> | |
<%= f.text_field :poster, placeholder: "Gif, Jpg ou PNG" %> | |
</p> | |
<p> | |
<%= f.submit %> | |
<%= link_to 'Cancelar', recipe_index_path %> | |
</p> | |
<% end %> | |
######################### A chamada da partial ########################### | |
# O nome do arquivo é _form.html.erb | |
# View new.html.erb # | |
<%= render 'form' %> | |
# View edit.html.erb | |
<%= render 'form' %> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment