2014-10-16 89 views
1

在更新記錄時遇到Rails應用程序的問題。每次我做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 
+0

當您在'rails console'中創建調用方法時,您會看到什麼? – 2014-10-16 18:36:55

回答

4

添加到edit方法中,所述控制器:

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

而意見的第一行更改爲:

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

P.S.你應該改變edit.html.erb和new.html.erb,所以如果新由於驗證失敗,它將保留正確填充的字段而不是清空它們。

+0

'edit'方法已經存在於控制器中:)。 – 2014-10-16 19:08:02

+1

正如@makhan指出的那樣,你觀察這個的原因是因爲你在表單中使用了'Flyer.new'。這意味着表單將始終用於Flyer的新實例。用'@ flyer'代替它可以做到這一點,從編輯方法 – 2014-10-16 19:14:31

+0

調用新方法時調用'@flyer = Flyer.new'和'@flyer = Flyer.find(params [:id])'輝煌。就是這樣。乾杯 – 2014-10-17 07:02:03

相關問題