This is a partial Rails application that has limited features. But use it as a reference.
Please make these changes to your Rails project BEFORE you made any code change. Make sure you RESTART your Rails server after making these changes.
You can just treat them as regular css files. You can change your Sublime setting at the lower right corner to view the .scss
files as CSS
. Then you can enjoy the CSS syntax highlighting.
Do the following:
- Comment out
gem 'coffee-rails', '~> 4.1.0'
in yourGemFile
. - Run
bundle install
. - If there are
.coffee
files already generated in yourapp/assets/javascripts
folder, you can simply rename them to.js
.
After you have made these changes, rails g controller
will generate .js
files instead of the default .coffee
for each controller you create.
Let's just do the simple installation for now. For more information, please check out the github page of the gem.
- Add
gem "twitter-bootstrap-rails"
in yourGemfile
. - Run
bundle install
. - Run
rails generate bootstrap:install static --no-coffeescript
The last command will do 3 things:
- Update
app/assets/stylesheets/application.css
. - Create
app/assets/stylesheets/bootstrap_and_overrides.css
. - Create
app/javascripts/bootstrap.js
.
The steps are quite simple. Check out the github page of the gem if you are interested.
- Add
gem "font-awesome-rails"
in yourGemfile
. - Run
bundle install
. - Add the following line to your
application.css
. Refer to the sample code if you are not sure where you should add this line.*= require font-awesome
You should start seeing the pattern by now. This is the gem's github page if you want to take a look.
- Add
gem "momentjs-rails"
in yourGemfile
. - Run
bundle install
. - Add the following line to your
application.js
. Refer to the same code if you are not sure where you should add this line.//= require moment
In the app/controllers/application_controller.rb
file, you can do this to ensure that the FIRST user from your users
table is always the current user.
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
#protect_from_forgery with: :exception
# Just use default (:null_session) for this super simple app
protect_from_forgery
before_action :set_current_user
private
def set_current_user
@current_user = User.find(1)
end
end
You can use @current_user
in ANY html.erb
page, for example:
Welcome back, <%=@current_user.first_name%>!!<br/>
In the sample code, if you need to use jQuery in any of the view pages generated for the HomeController
under the app/views/home/
folder, you should write the jQuery code in home.js
.
Likewise, for the views pages generated for the ReviewsController
under the app/views/reviews/
folder, you should write the jQuery code in reviews.js
.
You can refer to these files in the sample:
app/views/home/main.html.erb
app/assets/javascripts/home.js
app/views/reviews/index.html.erb
app/assets/javascripts/reviews.js
Compare these key files to the sample code and see if there are any differences:
app/assets/javascripts/application.js
app/assets/stylesheets/application.css
app/assets/stylesheets/bootstrap_and_overrides.css
app/controllers/application_controller.rb
Make sure you have ImageMagick installed. ImageMagick is a library that allow us to resize images in the server and do different kinds of image processing (black&white, transparencies, etc)
$ brew install imagemagick
Open Gemfile
and add this line at the end.
gem 'paperclip', '~> 4.3'
Then run bundle install
.
Next we need to create a migration to add an image
attribute to the Book
model. First we need to generate the migration file:
$ rails g migration add_image_to_books
Then we should update the migration file and add one line in the change
method:
class AddImageToBooks < ActiveRecord::Migration
def change
add_attachment :books, :image
end
end
And now we can do rake db:migrate
.
In app/models/book.rb
, add the following the lines of code underneath ### New Code ###
:
class Book < ActiveRecord::Base
has_many :reviews
has_many :users, through: :reviews
### New Code ###
has_attached_file :image, styles: {
medium: "300x300>",
thumb: "100x100>"
}
validates_attachment_content_type :image, content_type: /\Aimage\/.*\Z/
end
In the BooksController
, we need to add :image
as a whitelisted parameter inside the book_params
method.
class BooksController < ApplicationController
#
# Code now shown
#
private
def book_params
params.require(:book).permit(:title, :url, :author, :isbn, :image)
end
end
In app/views/books/_form.html.erb
, update the form_for
helper like this:
<%= form_for @book, html: { multipart: true} do |f| %>
And then, add a new field to the form:
<div class="field">
<%= f.label :image %><br>
<%= f.file_field :image %>
</div>
Finally, in app/views/home/main.html.erb
, change the code that display the book image with these
<a href="<%=new_book_review_path(book)%>">
<img class="thumbnail" src="<%=book.url.empty? ? burl(:medium) : book.url %>">
</a>
Before we test the image uploading, please make sure you have restarted your Rails server.
Have fun!!