Form is not saving data upon submission

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.

\r\n”, “category_id”=>“1”},
“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)
<%= simple_form_for(@article, html: {class: 'form-horizontal', :multipart => true}) do |f| %> <% if @article.errors.any? %>
<- omitted -> <% end %>
    <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

After attempting to create a record in the console. I get the output
below

(0.5ms) begin transaction
Article Exists (0.0ms) SELECT 1 AS one FROM “articles” WHERE
(“articles”.“id” != 12) AND “articles”.“slug” = ? LIMIT 1 [[“slug”,
“wow-the-movie-was-great”]]
(0.0ms) rollback transaction
false

I created 5 scoping methods inside of category.rb for targeting specific
categories. I don’t know why the records are not saving. I wrote the
form properly and routes correctly.

resources :articles do
member do
get ‘like’, to: ‘articles#like’
get ‘dislike’, to: ‘articles#unlike’
end
end

On Tue, Mar 8, 2016 at 4:00 PM, David W. [email protected]
wrote:

When I click the submit button, the form automatically refreshes the
page.

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

It should be pretty apparent that you’re accessing the “#new
method in your controller, NOT the “#create”, which would be
done via POST.

<%= simple_form_for(@article, html: {class: 'form-horizontal', :multipart => true}) do |f| %>

?! You’re creating a form tag within a form tag - not valid HTML in
any way. And that outer form has no method or action attributes, so
not much is ever going to happen, as you see :slight_smile:


Hassan S. ------------------------ [email protected]
http://about.me/hassanschroeder
twitter: @hassan
Consulting Availability : Silicon Valley or remote

Hassan S. wrote in post #1182051:

On Tue, Mar 8, 2016 at 4:00 PM, David W. [email protected]
wrote:

?! You’re creating a form tag within a form tag - not valid HTML in
any way. And that outer form has no method or action attributes, so
not much is ever going to happen, as you see :slight_smile:


Hassan S. ------------------------ [email protected]
http://about.me/hassanschroeder
twitter: @hassan
Consulting Availability : Silicon Valley or remote

I fixed the form, I didn’t realize that I was still using the

approach that I needed for Stripe. Which doesn't pass the form to the database, but transfers the information over the internet back to stripe.

I have one problem though, the category_id is not being found when I
submit the form.

Thanks, I fixed the form. We can close this issue.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs