Class: MicropostsController

Inherits:
ApplicationController show all
Defined in:
lib/generators/chapter11_3/solutions/templates/app/controllers/microposts_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#after_sign_in_path_for

Instance Method Details

#createObject



4
5
6
7
8
9
10
11
12
# File 'lib/generators/chapter11_3/solutions/templates/app/controllers/microposts_controller.rb', line 4

def create
  @micropost  = current_user.microposts.build(params[:micropost])
  if @micropost.save
    flash[:success] = "Micropost created!"
    redirect_to root_path
  else
    render 'pages/home'
  end
end

#destroyObject



14
15
16
17
18
# File 'lib/generators/chapter11_3/solutions/templates/app/controllers/microposts_controller.rb', line 14

def destroy
  # .try will call the method given, unless the caller is nil
  current_user.microposts.find_by_id(params[:id]).try(:destroy)
  redirect_to :back
end