Search code examples
ruby-on-railsformsscaffold

Rails scaffold form not updating database


I am using scaffold in rails to create an application where users can submit ideas, and then all ideas will be listed on a main page. I'm currently just trying to get the submit and listing functionality, however the form that scaffold generated in the _form.html.erb file doesn't seem to actually submit anything or update the database. I.e., I get neither the message "Idea submitted successfully", nor the message "Error submitting idea". It just refreshes the form page.

I am wondering whether it is a problem with my routes.rb file or something, because after spending a lot of time on the internet I feel like most of my code should work.

idea.rb

class Idea < ApplicationRecord    
  belongs_to :user
  belongs_to :category

  has_many :comments

  validates :title, :user, :anonymous, :contents, presence: true
  validates :deleted, inclusion: { in: [true, false]}

  scope :alive, -> { where deleted: false } 

  def children
    Comment.where(idea_id: id, deleted: false)
  end    
end

ideas_controller.rb

class IdeasController < ApplicationController

  def index
    @ideas = Idea.alive.paginate(page: params[:page], per_page: 20)
  end

  def show
    @idea = Idea.find(params[:id])
    @comments = @idea.children
    @reply = Comment.new
  end

  def new
    @idea = Idea.new
    @categories = Category.all.map{|c| [ c.name, c.id ] }
  end

  def new_comment
    @idea = Idea.find(params[:id])
    @comment = Comment.new
  end

  def create
    @idea = Idea.new(idea_params)
    if @idea.save
      redirect_to @idea, alert: "Idea submitted successfully."
    else
      redirect_to new_idea_path, alert: "Error submitting idea."
    end
  end

  def idea_params
    params.require(:idea).permit(:title, :contents, :anonymous, :category)
  end   
end

_form.html.erb:

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

      <ul>
      <% idea.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

<div class="field">
    <%= f.label :title %><br />
    <%= f.text_field :title %>
</div>
<div class="field">
    <%= f.check_box(:anonymous) %>
    <%= f.label(:anonymous, "Hide my name") %>
</div>
<div class="field">
    <%= f.label :contents %><br />
    <%= f.text_area :contents %>
</div>
      <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

create_ideas.rb:

class CreateIdeas < ActiveRecord::Migration[5.0]
  def change
    create_table :ideas do |t|

    t.string :title
    t.text :contents
    t.boolean "anonymous", limit: 1, default: false
    t.boolean "deleted", limit: 1, default: false

    t.timestamps
    end
  end
end

routes.rb:

Rails.application.routes.draw do
  resources :ideas
end

EDIT: there is no error trace per se, because I don't see anything--the form page just reloads. However, here is the output from the server log:

Started POST "/ideas" for ::1 at 2017-01-28 20:40:07 -0500
Processing by IdeasController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"5ooCTQZKBv1f1+SlZyl/7tkJzLx95EpckoPBpB710JrikRJqt8wOdE0GT3bjBl1m6wIxKQhzfXmKRrvkV+RHeA==", "idea"=>{"title"=>"sadfsd", "anonymous"=>"1", "contents"=>"sdafasdf"}, "commit"=>"Create Idea"}
   (0.3ms)  begin transaction
   (0.1ms)  rollback transaction
Redirected to http://localhost:3000/ideas/new
Completed 302 Found in 26ms (ActiveRecord: 0.4ms)

Solution

  • You have validation and you are violating :user presence.

    Your ideas table doesn't have a user_id column, which makes belongs_to :user relation not valid.

    You need change your migration

    class CreateIdeas < ActiveRecord::Migration[5.0]
      def change
        create_table :ideas do |t|
    
          t.string :title
          t.text :contents
          t.boolean "anonymous", limit: 1, default: false
          t.boolean "deleted", limit: 1, default: false
          t.integer :user_id, index: true
    
          t.timestamps
        end
      end
    end
    

    And run your migration again, rake db:reset

    Alternatively, you can create another migration to add the user_id column.

    In your controller create, make sure you have current_user, or any way to get the user who is creating the idea, and change to

    @idea = current_user.ideas.new(idea_params)