This Snipp is an example of how the models work with the use of good practices
#CONTROLLER
class ItemsController < ApplicationController
def publish
if @item.publish
flash[:notice] = "Your item's published!"
else
flas[:notice] = "There was an error"
end
redirect_to @item
end
end
#MODEL
class Item < ActiveRecord::Base
belongs_to :user
def publish
if !is_approved? || user == "Juanito"
return false
end
self.published_on = Time.now
self.save
end
end
Encapsular el metodo simplifica el código y mejora la legibilidad
#CONTROLLER
class ItemsController < ApplicationController
def publish
if @item.is_approved? && @item.user != "Juanito"
@item.published_on = Time.now
if @item.save
flash[:notice] = "Your item's published!"
else
flash[:notice] = "There was some error"
end
else
flas[:notice] = "There was an error"
end
redirect_to @item
end
end
Estos son los problemas con los que cuenta el código.
Tambien: