Code Monkey home page Code Monkey logo

user-songs's Introduction

User Songs

This rails app is a one model app that has a single Song model. A song has a title.

Your job is to implement best security practices for this app:

User login and authorization with devise.

We will follow instuctions from the devise website: https://github.com/plataformatec/devise

Add the gem devise to the Gemfile

gem 'devise'

Install it

bundle install
rails db:create

Run the gem's script files so it can generate the default files in the rails app

rails generate devise:install

Create the devise user model:

rails generate devise user

Link User to a new foreign key column in songs:

rails g migration AddUserToSongs user:references
rails db:migrate

Generate the default devise view files:

rails g devise:views

Devise might ask you to copy the secret devise key into the initializer file: config/initializers/devise.rb

Set up authorization:

Add a before action filter to the controller:

before_action :authenticate_user!, :except => [ :show, :index ]

Restrict the new link in the songs index with the devise helper

<% if user_signed_in? %>
<%= link_to 'New Song', new_song_path %>
<% end %>

Other devise helpers:

current_user

Make a breakpoint (byebug) in the index song controller method.

Inside the console see the value of current_user

current_user.id

user_session

See the current value of user_session

user_session

Set something in the user session

user_session[:song_cart] = "Single Ladies"

Use c to continue out of the breakpoint.

Make another request.

See the value of user_session has been retained.

Adding user associations

Change both model files

app/models/song.rb

belongs_to :user

app/models/user.rb

has_many :song

Assign the logged in user as the song creator in app/controllers/songs_controller.rb

def create
  @song = Song.new(song_params)

  @song.user = current_user

  if @song.save
    redirect_to @song
  else
    render 'new'
  end
end

Logout Link

<%= link_to 'log out', destroy_user_session_url, method: :delete %>

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.