When I click the submit button, the form automatically refreshes the
page. The console doesn’t show any errors related to strong parameters
as stated below. I will post the controller, _form view and model
information
Started GET
“/articles/new?utf8=%E2%9C%93&authenticity_token=GP%2Fm4GX2Z2CUjuFt5QJOEEcIcEtKzsd9nPGQ6OfmMQn2yiUHqwoGPvzQiu2hSiPK0V8%2Br1r%2F0TfC5zZM26Y2VA%3D%3D&article%%5Bcategory_id%5D=1&commit=Create+Article”
for 127.0.0.1 at 2016-03-08 18:30:00 -0500
Processing by ArticlesController#new as HTML
Parameters: {“utf8”=>“✓”,
“authenticity_token”=>“GP/m4GX2Z2CUjuFt5QJOEEcIcEtKzsd9nPGQ6OfmMQn2yiUHqwoGPvzQiu2hSiPK0V8+r1r/0TfC5zZM26Y2VA==”,
“article”=>{“title”=>“Awesome Title!”,
“subtitle”=>“I cannot wait to get back home”,
“prev_img”=>“movies.jpg”, “body”=>“
Wow. This is
absolutely brilliant and beautiful.
“commit”=>“Create Article”}
Category Load (0.5ms) SELECT “categories”.* FROM “categories”
Rendered articles/_form.html.erb (19.5ms)
Rendered articles/new.html.erb within layouts/application (22.5ms)
User Load (0.0ms) SELECT “users”.* FROM “users” WHERE “users”.“id” =
? ORDER BY “users”.“id” ASC LIMIT 1 [[“id”, 5]]
Rendered navigation/_navbar.html.erb (9.5ms)
Completed 200 OK in 2011ms (Views: 2009.3ms | ActiveRecord: 0.5ms)
<fieldset class="form-group">
<%= f.input :title, class: 'form-control', placeholder:
“Headline” %>
<fieldset class="form-group">
<%= f.input :subtitle, class: 'form-control', placeholder:
‘Description’ %>
<fieldset>
<i class="fa fa-picture-o"></i>
<img id="img_prev" width=370 height=180 src="#" alt=""
class=“img-thumbnail hidden”/>
<%= f.label :prev_img, ‘Select a preview image for your
article’ %>
<%= f.file_field :prev_img, class: ‘form-control-file’, id:
‘img-upload’ %>
<div class="form-inputs">
<%= f.cktext_area :body %>
</div>
<div class="field">
<%= f.label :category_id, 'Select a category' %>
<%= f.collection_select :category_id, Category.all, :id, :name
%>
<%= f.submit ‘Submit Info’, class: ‘btn btn-primary’ %>
<% end %>
class ArticlesController < ApplicationController
before_action :set_article, only: [:show, :edit, :update, :destroy]
def index
@Articles = Article.all
end
def show
end
def new
@article = Article.new
end
def edit
end
def create
@user = current_user
@article = @user.articles.build(article_params)
@category = Category.find(params[:category_id])
@article.category = @category
respond_to do |format|
if @article.save
format.html { redirect_to @article, notice: ‘Article was
successfully created.’ }
format.json { render :show, status: :created, location: @article
}
format.js
else
format.html { render :new }
format.json { render json: @article.errors, status:
:unprocessable_entity }
end
end
end
def update
respond_to do |format|
if @article.update(article_params)
format.html { redirect_to @article, notice: ‘Article was
successfully updated.’ }
format.json { render :show, status: :ok, location: @article }
else
format.html { render :edit }
format.json { render json: @article.errors, status:
:unprocessable_entity }
end
end
end
def destroy
if @article.user == current_user
@article.destroy
redirect_to articles_path
flash[:success] = “Article has been removed”
else
redirect_to :back
flash[:danger] = “Unable to remove Article”
end
respond_to do |format|
format.html
format.json { head :no_content }
end
end
private
def set_article
@article = Article.find(params[:id])
end
def article_params
params.require(:article).permit(:title, :body, :subtitle, :prev_img,
:category_id, :description)
end
end
class Article < ActiveRecord::Base
belongs_to :user
belongs_to :category
has_many :comments, as: :commentable, dependent: :destroy
default_scope -> { order(created_at: :desc) }
mount_uploader :prev_img, PrevImgUploader
validates :prev_img, presence: true
validates :body, presence: true
validates :title, presence: true
validates :subtitle, presence: true
end
class Category < ActiveRecord::Base
has_many :articles
end