Search code examples
ruby-on-railsdatabaseruby-on-rails-4objectsqlite3-ruby

Rails: Object saving in console but not within app


I know this must be a dumb error somewhere, but I've been going in circles trying to figure out why my objects only save within the console, and not in the app itself.

Anyone have an idea?

Here's the code:

Controller:

 class JobsController < ApplicationController
  def index
    @jobs = Job.all
  end

  def new
    @job = Job.new
  end

  def create
    @job = Job.new(job_params)

    if @job.save
      redirect_to jobs_path, flash: { notice: "You've successfully created a job." }
    else
      redirect_to new_job_path, flash: { alert: @job.errors.messages }
    end
  end

  def edit
    @job = Job.find(params[:id])
  end

  def update
    @job = Job.find(params[:id])
    if @job.update
      redirect_to jobs_path, notice: "You've updated a job."
    else
      redirect_to edit_job_path, alert: @job.errors.messages
    end
  end

  def destroy
    @job = Job.find(params[:id])
    if @job.destroy
      redirect_to jobs_path, notice: "You've deleted a job."
    else
      redirect_to jobs_path, alert: @job.errors.messages
    end
  end

  private

  def job_params
    params.require(:job).permit(:poster, :category, :location, :status)
  end
end

And the view:

<h1>Create job</h1>
<form class="form">
  <%= form_for @job do |f| %>
    <%= f.text_field :poster, placeholder: "Poster", onclick: "placeholder = ''", onblur: "placeholder = 'Poster'", autofocus: true, class: "form-group" %><br>
    <%= f.text_field :category, placeholder: "Category", onclick: "placeholder = ''", onblur: "placeholder = 'Category'", class: "form-group" %><br>
    <%= f.text_field :location, placeholder: "Location", onclick: "placeholder = ''", onblur: "placeholder = 'Location'", class: "form-group" %><br>
    <%= f.select :status, ['New', 'Pending', 'Complete'],placeholder: "Status", onclick: "placeholder = ''", onblur: "placeholder = 'Status'", class: "form-group" %><br><br>
    <%= f.submit "Submit", class: "btn btn-secondary" %>
  <% end %>
</div>

And the log:

Started GET "/jobs/new?utf8=%E2%9C%93&authenticity_token=FStlZTVRMKg%2Bw3Zkw5%2FdpP1w5I%2Fl4vxlQPwAGKqRjIM704p6vXWXEe9z4GmdoB4FJirP6AzUJRkCPEcEwmVNjQ%3D%3D&job%5Bposter%5D=jill&job%5Bcategory%5D=jill&job%5Blocation%5D=jill&job%5Bstatus%5D=New&commit=Submit" for ::1 at 2017-04-16 15:16:08 -0400
Processing by JobsController#new as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"FStlZTVRMKg+w3Zkw5/dpP1w5I/l4vxlQPwAGKqRjIM704p6vXWXEe9z4GmdoB4FJirP6AzUJRkCPEcEwmVNjQ==", "job"=>{"poster"=>"jill", "category"=>"jill", "location"=>"jill", "status"=>"New"}, "commit"=>"Submit"}
  Rendered jobs/new.html.erb within layouts/application (0.8ms)
Completed 200 OK in 13ms (Views: 12.1ms | ActiveRecord: 0.0ms)

Solution

  • This line is breaking your form

    <form class="form">
    

    You can't have nested forms in HTML. So <form> from your form_for definition is not interpreted as it should.

    Also, there's a dangling </div>.