Skip to content

Reviews

bj0rno edited this page Jun 4, 2013 · 4 revisions

In this part of the documentation I will explain the review controller with his views and the model. The review controller makes it possible to write a review under each article. How this all works will be explained below. The entire code will be explained line by line. I'll start with the model then the controller and as last the views.

1.The model of the review

class Review < ActiveRecord::Base
  belongs_to :[article]
  belongs_to :user
  attr_accessible :author, :content, :rating, :title, :article_id, :review, :user_id, :picture

  validates :user_id, :presence => true

end
class Review < ActiveRecord::Base

The first line defines the class Review. The class review inherits from ActiveRecord::Base. ActiveRecord tells the system that it's dealing with the base(in this case a base-model).

belongs_to :article

The second line explains where the review model belongs to. In this case it belongs to the Article model.

belongs_to :user

The third line explains that the review model also belongs to the user model. Because the user writes a review under an [article] it has to know which user writes beneath which Article.

 attr_accessible :author, :content, :rating, :title, :article_id, :review, :user_id, :picture

The fourth line describes which attributes are accessible for the review model. In this case the there are eight attributes accessible. I'll will describe them below:

  • :author = string. This stores the name of the author of an Article.

  • :content = text. This stores the content of an Article.

  • :rating = integer. This stores a number with the rating of an Article.

  • :title = string. This stores the title of an Article.

  • :article_id = integer. This stores the id of an Article.

  • :user_id = integer. This stores the id of a user where the review will be coupled.

  • :picture = string. The picture will be a string.

    validates :user_id, :presence => true

The fifth line looks if the presence of the user_id is true. If this is not the case it will no longer execute the code.

2.The review controller

The controller stores all the variables and actions that ensure that the review works. In this paragraph I will take this controller to a smaller level and explain each of his functions.

class ReviewsController < ApplicationController
  before_filter :authenticate_user!, only: [:new, :create, :edit, :save]
  # GET /reviews
  # GET /reviews.json

  def reviews
    @reviews = Review.all

    respond_to do |format|
      format.json { render json: @reviews.to_json(:include => { :user => {:except => [:admin, :email,  
           :updated_at, :created_at]  } })}
    end
  end
  
  def create
   @article = Article.find(params[:article_id])
   @review = @article.reviews.create(params[:review])
    
    redirect_to article_path(@article)

  end

  # DELETE /reviews/1
  # DELETE /reviews/1.json
  def destroy
    @article = Article.find(params[:article_id])
    @review = @article.reviews.find(params[:id])
    @review.destroy

    redirect_to article_path(@article)
    end
end

class ReviewsController < ApplicationController

The review controller is wrapped in a class called ArticleController this class is part of the the applications controller. In the Application Controller basic functions are set and the anti-forgery things are stored there as well. More info on the Application controllers core functions and Application Controller can be found in the Application article.

before_filter :authenticate_user!, only: [:new, :create, :edit, :save]

This first line of code ensures that the user is signed in before visiting some pages. The function :authenticate_user! is created by the devise gem. This function makes sure that the user is logged-in when he visits the new, edit, create or save page.

before_filter :authenticate_user!, only: [:new, :create, :edit, :save]

Def reviews

def reviews

The fifth line of code is the begin of the review method. This method includes all the variables and the jason files that are rendered.

@reviews = Review.all

The @review variable collects all the reviews that are stored in the database.

respond_to do |format|
      format.json { render json: @reviews.to_json(:include => { :user => {:except =>                   
[:admin, :email,  :updated_at, :created_at]  } })}

The json file is a file that stores an array of variables. The json file is generated from @reviews and includes the users information that posted the different reviews. The json file can be called by Ajax, Javascripts and JQuery.

Def create

def create

The create method includes all the variables that are needed to create a review.

@article = Article.find(params[:article_id])

The @article variable search in Article database to the :article_id column.

@review = @article.reviews.create(params[:review])

In this method the @review variable search for a specific Article then the reviews database and makes a create action. Then in the params he searches for a column named :review and stores the information in that column in the reviews database.

redirect_to article_path(@article)

When the review is created the redirect_to ensures that the site goes back to the specific Article where you can see the created review.

Def destroy

def destroy

The method destroy makes sure that the user can delete the review.

@article = Article.find(params[:article_id])

The @article variable searches in Article database to the :article_id column

@review = @article.reviews.find(params[:id])

The @review variable in the destroy method finds the :article_id from the @article variable. Next he searches the id of the review by the :id column in the reviews database. This :id column is coupled to the :article_id in the Article database.

  @review.destroy

The @review.destroy variable ensures that the review will be destroyed along with the :article_id in the Article database.

  redirect_to article_path(@article)

When this process is completed it goes back to the specific Article by the redirect_to action.

3.The views

The reviews has five view pages: _form, edit, new, show. All of these pages with there functions are described below.

form view

<%= form_for(@review) do |f| %>
  <% if @review.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@review.errors.count, "error") %> prohibited this review from being saved:</h2>

      <ul>
      <% @review.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :title %><br />
    <%= f.text_field :title %>
  </div>
  <div class="field">
    <%= f.label :author %><br />
    <%= f.text_field :author %>
  </div>
  <div class="field">
    <%= f.label :content %><br />
    <%= f.text_area :content %>
  </div>
  <div class="field">
    <%= f.label :article %><br />
    <%= f.text_field :article %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

End of the form.

<%= form_for(@review) do |f| %>

The form_for searches for the @review variable which stores the information of the review that is created. Then he loops through all of the reviews that are stored in the database.

  <% if @review.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@review.errors.count, "error") %> prohibited this review from being saved:</h2>

      <ul>
      <% @review.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>

if the user filled in the form wrong the form sends an error message.

<% @review.errors.full_messages.each do |msg| %>

The each loop goes through all of the available error messages. And when an error encounters the msg variable will pop up with an error message.

  <div class="field">
    <%= f.label :title %><br />
    <%= f.text_field :title %>
  </div>

Every time the loop is running he does f. f.label stands for the title of the field. When f.text_field :title is called he is searching for the :title column that is stored in the reviews database. When you write a title it is stored in the reviews database.

   <div class="field">
    <%= f.label :content %><br />
    <%= f.text_area :content %>
  </div>

By content he does the same thing as described above. The only difference is that the f.text_field is changed in f.text_area. text_area is a block where you can write a large amount of text.

   <div class="actions">
    <%= f.submit %>
   </div>

The f.submit stands for a button and has a post request in it. When the user press the submit button the form will be send to the database and stores all the information in the specific columns.

Edit view

    <h1>Editing review</h1>

<%= render 'form' %>
    <%= link_to 'Show', @review %> |
<%= link_to 'Back', reviews_path %>

This page ensures that the review can be edited.

    <%= render 'form' %>

The first line renders the form.

    <%= link_to 'Show', @review %> |
<%= link_to 'Back', reviews_path %>

The last two lines creating a link to show the review or go back to all the reviews.

New view

    <h1>New review</h1>

    <%= render 'form' %>
    <%= link_to 'Back', reviews_path %>

When the new review link is clicked the form is rendered and the user can make a new review. The link_to ensures that the user can go back to the main page.

Show view