Skip to content

Commit

Permalink
Regenerate articles controller with simple code, remove attr accessib…
Browse files Browse the repository at this point in the history
…le call and fix deprecation warning
  • Loading branch information
carlosantoniodasilva committed May 8, 2013
1 parent b4bedbe commit 1636020
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 57 deletions.
10 changes: 4 additions & 6 deletions app/controllers/articles/comments_controller.rb
Expand Up @@ -8,12 +8,10 @@ def create
@comment = Comment.new(params[:comment])
@comment.article = @article

respond_to do |format|
if @comment.save
format.html { redirect_to @article, :notice => 'Comment was successfully created.' }
else
format.html { render "articles/show" }
end
if @comment.save
redirect_to @article, :notice => 'Comment was successfully created.'
else
render "articles/show"
end
end

Expand Down
71 changes: 23 additions & 48 deletions app/controllers/articles_controller.rb
@@ -1,83 +1,58 @@
class ArticlesController < ApplicationController
before_action :set_article, only: [:show, :edit, :update, :destroy]

# GET /articles
# GET /articles.json
def index
@articles = Article.all

respond_to do |format|
format.html # index.html.erb
format.json { render :json => @articles }
end
end

# GET /articles/1
# GET /articles/1.json
def show
@article = Article.includes(:comments).find(params[:id])

respond_to do |format|
format.html # show.html.erb
format.json { render :json => @article }
end
end

# GET /articles/new
# GET /articles/new.json
def new
@article = Article.new

respond_to do |format|
format.html # new.html.erb
format.json { render :json => @article }
end
end

# GET /articles/1/edit
def edit
@article = Article.find(params[:id])
end

# POST /articles
# POST /articles.json
def create
@article = Article.new(params[:article])
@article = Article.new(article_params)

respond_to do |format|
if @article.save
format.html { redirect_to @article, :notice => 'Article was successfully created.' }
format.json { render :json => @article, :status => :created, :location => @article }
else
format.html { render :action => "new" }
format.json { render :json => @article.errors, :status => :unprocessable_entity }
end
if @article.save
redirect_to @article, notice: 'Article was successfully created.'
else
render action: 'new'
end
end

# PUT /articles/1
# PUT /articles/1.json
# PATCH/PUT /articles/1
def update
@article = Article.find(params[:id])

respond_to do |format|
if @article.update_attributes(params[:article])
format.html { redirect_to @article, :notice => 'Article was successfully updated.' }
format.json { head :ok }
else
format.html { render :action => "edit" }
format.json { render :json => @article.errors, :status => :unprocessable_entity }
end
if @article.update(article_params)
redirect_to @article, notice: 'Article was successfully updated.'
else
render action: 'edit'
end
end

# DELETE /articles/1
# DELETE /articles/1.json
def destroy
@article = Article.find(params[:id])
@article.destroy
redirect_to articles_url, notice: 'Article was successfully destroyed.'
end

respond_to do |format|
format.html { redirect_to articles_url, :notice => 'Article was successfully deleted.' }
format.json { head :ok }
private
# Use callbacks to share common setup or constraints between actions.
def set_article
@article = Article.find(params[:id])
end

# Only allow a trusted parameter "white list" through.
def article_params
params[:article].permit!
end
end
end
2 changes: 0 additions & 2 deletions app/models/comment.rb
@@ -1,6 +1,4 @@
class Comment < ActiveRecord::Base
attr_accessible :name, :body

belongs_to :article

validates_presence_of :name, :body, :article
Expand Down
2 changes: 1 addition & 1 deletion app/views/articles/index.html.erb
Expand Up @@ -25,7 +25,7 @@
<td><%= article.content %></td>
<td>
<%= link_to 'Edit', edit_article_path(article) %>
<%= link_to 'Destroy', article, :confirm => 'Are you sure?', :method => :delete %>
<%= link_to 'Destroy', article, :data => { :confirm => 'Are you sure?' }, :method => :delete %>
</td>
</tr>
<% end %>
Expand Down

0 comments on commit 1636020

Please sign in to comment.