3therk1ll
3therk1ll

Reputation: 2421

Rails creating new record instead of updating

Having a problem with a Rails app when updating a record. Each time I do, Rails creates a new record rather than editing the one selected to be updated.

I've notices that whether I browse to edit or create, the form is rendered with a Create Flyer button. So I'm not sure if the problem is something internal in Rails connected to that.

I've attached here everything I can think that's relevant. Any other info let me know.

The objects themselves are flyers with the following values:

ActiveRecord::Schema.define(version: 20141016141700) do

  create_table "flyers", force: true do |t|
    t.string   "event_name"
    t.string   "location"
    t.string   "genre"
    t.string   "date"
    t.string   "frequency"
    t.string   "tags"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

end

flyers_controller.rb

class FlyersController < ApplicationController

  def index
    @flyers = Flyer.all
  end

  def new
    @flyer = Flyer.new
  end

  def create
    Flyer.create flyer_params
    redirect_to '/flyers'
  end

  def edit
    find_params
  end

  def update
    find_params
    @flyer.update flyer_params
    redirect_to '/flyers'
  end

private 

  def flyer_params
    params[:flyer].permit(:event_name, :location, :genre, :tags, :date, :frequency)
  end

  def find_params
    @flyer = Flyer.find params[:id]
  end

end

Both the edit.html.erb and new.html.erb are using the following render:

<%= form_for Flyer.new do |f| %>

    <%= f.label :event_name %>
    <%= f.text_field :event_name %>

    <%= f.label :location %>
    <%= f.text_field :location %>

    <%= f.label :genre%>
    <%= f.text_field :genre %>

    <%= f.label :tags %>
    <%= f.text_field :tags %>

    <%= f.label :date %>
    <%= f.text_field :date %>

    <%= f.label :frequency %>
    <%= f.text_field :frequency %>


    <%= f.submit %>

<% end %>

flyers_feature_spec.rb

require 'rails_helper'

describe 'flyer upload page' do
    context 'no flyers uploaded' do
        it 'should display a notification' do
            visit '/flyers'
            expect(page).to have_content 'No flyers added'
        end
    end

    context 'adding a flyer' do
        it 'should be listed on the index' do
            visit '/flyers'
            click_link 'Add New Flyer'
            fill_in 'Event name', with: 'Hospitality'
            fill_in 'Location', with: 'Brighton'
            fill_in 'Genre', with: 'Drum and bass, obvs'
            fill_in 'Tags', with: 'liquid'
            fill_in 'Date', with: '12/12/14'
            fill_in 'Frequency', with: 'Weekly'

            click_button 'Create Flyer'
            expect(current_path).to eq '/flyers'
            expect(page). to have_content 'Hospitality'
        end
    end


    context 'with existing flyers' do

        before do
            Flyer.create(event_name: 'Hospitality')
        end

        describe 'editing a flyer' do
            it 'should update flyer details' do
                visit '/flyers'
                click_link 'Edit'
                fill_in 'Event name', with: 'Hospitality v2'
                click_button 'Update'

                expect(page).to have_content 'Hospitality v2'
            end
        end

    end
end

Upvotes: 1

Views: 2959

Answers (1)

makhan
makhan

Reputation: 4009

Add to edit method in the controller:

@flyer = Flyer.find(params[:id])

And change the first line of the views to:

<%= form_for @flyer do |f| %>

p.s. you should change both edit.html.erb and new.html.erb, so if new fails because of validation, it will preserve the correctly filled fields instead of emptying them.

Upvotes: 4

Related Questions