Ruby on rails Rails创建新记录而不是更新

Ruby on rails Rails创建新记录而不是更新,ruby-on-rails,ruby,rails-activerecord,Ruby On Rails,Ruby,Rails Activerecord,更新记录时Rails应用程序出现问题。 每次我这样做时,Rails都会创建一个新记录,而不是编辑要更新的选定记录 我注意到,无论是浏览编辑还是创建,表单都会使用create Flyer按钮呈现。因此,我不确定问题是否出在与之相连的Rails内部 我在这里附上了所有我认为相关的东西。其他任何信息都告诉我 对象本身是具有以下值的传单: ActiveRecord::Schema.define(version: 20141016141700) do create_table "flyers", f

更新记录时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
传单\u功能\u规格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

添加到控制器中的编辑方法:

@flyer = Flyer.find(params[:id])
并将视图的第一行更改为:

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


p、 s.您应该同时更改edit.html.erb和new.html.erb,这样,如果new由于验证而失败,它将保留正确填写的字段,而不是清空它们。

添加到控制器中的
edit
方法:

@flyer = Flyer.find(params[:id])
并将视图的第一行更改为:

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


p、 您应该同时更改edit.html.erb和new.html.erb,因此如果new由于验证而失败,它将保留正确填充的字段,而不是清空它们。

rails控制台中创建调用方法时,您看到了什么?
rails控制台中创建调用方法时,您看到了什么?正如@makhan正确指出的,您之所以观察到这一点,是因为您正在表单中使用
Flyer.new
。这意味着表单将始终用于新的传单实例。将其替换为
@flyer
会起作用,因为从新方法调用时
@flyer=flyer.new
,从编辑方法调用时
@flyer=flyer.find(params[:id])
。就这样。正如@makhan正确指出的,你之所以观察到这一点,是因为你在表单中使用了
Flyer.new
。这意味着表单将始终用于新的传单实例。将其替换为
@flyer
会起作用,因为从新方法调用时
@flyer=flyer.new
,从编辑方法调用时
@flyer=flyer.find(params[:id])
。就这样。干杯,伙计们