Rails创建新记录而不是更新

时间:2014-10-16 18:33:23

标签: ruby-on-rails ruby rails-activerecord

更新记录时出现Rails应用程序问题。 每次我这样做,Rails都会创建一个新记录,而不是编辑一个选择要更新的记录。

我注意到无论是浏览还是创建,都会使用Create Flyer按钮呈现表单。所以我不确定问题是否是Rails内部的问题。

我附上了我认为相关的一切。任何其他信息都让我知道。

对象本身是具有以下值的传单:

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

edit.html.erbnew.html.erb都使用以下渲染:

<%= 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

1 个答案:

答案 0 :(得分:4)

在控制器中添加edit方法:

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

并将视图的第一行更改为:

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

P.S。你应该更改edit.html.erb和new.html.erb,所以如果new因验证而失败,它将保留正确填充的字段而不是清空它们。