0

私は奇妙な問題で立ち往生しています。正しく作成および削除されたリコール値はほとんどありませんが、値を編集して保存しようとすると、新しいリコールが作成されました。

ここに編集/更新用のコントローラーがあります

def edit
  @recall = Recall.find(params[:id])
end

def update
      @recall = Recall.find(params[:id])
      if @recall.update_attributes(params[:recall])
        # Handle a successful update.
        flash[:success] = "Recall updated"
        redirect_to '/recalls'
      else
        render 'edit'
      end
    end

    def show
        @user = Recall.find(params[:id])
    end

私のedit.html.erbは次のとおりです

<%= form_for(@recall) do |f| %>



        <%= f.label :Category, "Category" %>

      <div class="control-group">
      <div class="controls">
        <%= f.select :Category,options_for_select(["Consumer Products",
          "Foods, Medicines, Cosmetics",
          "Meat and Poultry Products",
          "Motor Vehicles",
          "Child Safety Seats",
          "Tires",
          "Vehicle Emissions",
          "Environmental Products",
          "Boats and Boating Safety"]), {:style => "height:40px"} %>
</div>
</div>

      <div class="form-inline">
        <%= f.label :Title, "Title" %>
      </div>
      <%= f.text_field :Title %>

      <div class="form-inline">
        <%= f.label :Summary, "Summary" %>
      </div>
      <%= f.text_field :Summary %>

      <div class="form-inline">
        <%= f.label :Details, "Details" %>
      </div>
      <%= f.password_field :Details %>

      <%= f.submit "Save changes", class: "btn btn-large btn-primary" %>
    <% end %>

私がどこで間違ったのか教えてください。定義しようとしまし:action => 'edit'たが、うまくいきませんでした。前もって感謝します

EDIT rake routes 出力はこちら

users GET    /users(.:format)            users#index
                 POST   /users(.:format)            users#create
        new_user GET    /users/new(.:format)        users#new
       edit_user GET    /users/:id/edit(.:format)   users#edit
            user GET    /users/:id(.:format)        users#show
                 PUT    /users/:id(.:format)        users#update
                 DELETE /users/:id(.:format)        users#destroy
        sessions POST   /sessions(.:format)         sessions#create
     new_session GET    /sessions/new(.:format)     sessions#new
         session DELETE /sessions/:id(.:format)     sessions#destroy
            root        /                           administrator_pages#home
          signup        /signup(.:format)           users#new
          signin        /signin(.:format)           sessions#new
         signout DELETE /signout(.:format)          sessions#destroy
           about        /about(.:format)            administrator_pages#about
         recalls        /recalls(.:format)          administrator_pages#Recalls
          recall        /recall(.:format)           administrator_pages#create
         destroy        /destroy(.:format)          administrator_pages#destroy
            edit        /edit(.:format)             administrator_pages#edit
       users_new GET    /users/new(.:format)        users#new
  paid_user_paid GET    /paid_user/paid(.:format)   paid_user#paid
basic_user_basic GET    /basic_user/basic(.:format) basic_user#basic
   search_Search GET    /search/Search(.:format)    search#Search

これが私routes.rbの です。レーキ ルートと私の routes.rb を見ると、何か問題があることがわかります。しかし、問題を理解することはできません

 resources :users
  resources :sessions, only: [:new, :create, :destroy]

  root to: 'administrator_pages#home'

  match '/signup',  to: 'users#new'
  match '/signin',  to: 'sessions#new'
  match '/signout', to: 'sessions#destroy', via: :delete

  match '/about',   to: 'administrator_pages#about'
  match '/recalls',  to: 'administrator_pages#Recalls'

  match 'recall',  to:'administrator_pages#create'
  match 'destroy', to: 'administrator_pages#destroy'
  match 'edit', to: 'administrator_pages#edit'

#  get "administrator_pages/Recalls"
  get "users/new"

  get "paid_user/paid"

  get "basic_user/basic"

  get "search/Search"
4

3 に答える 3

1

recall編集/更新アクションでは次のことを行うため、ルート内で a の id を送信する必要があります。

@recall = Recall.find(params[:id])

編集用のルートは match 'edit/:id', to: 'administrator_pages#edit', as: 'edit_recall' 次のようになります。更新用にもう 1 つ必要なように見えますが、method: :put

上記のルートを使用すると、次のような URL になります。

localhost:3000/3/edit #3 is the id of recall

ただし、administrator_pages先に進みたい場合は、ルートを変更する必要があります。

match 'administrator_pages/recall/edit/:id', to: 'administrator_pages#edit', as: 'edit_recall'

結果:

localhost:3000/administrator_pages/recall/3/edit

リクエスト時に id のパラメーターが送信され、それをRecall.find(params[:id])コントローラーで使用できます。updateまた、メソッドを使用してアクションのルートも描く必要がありますput

より良い解決策として、ルートにリソース リコールを追加します。

resources :recalls

これにより、リコールedit, new, show,などを処理するために必要なすべてのルートが得られます..

于 2013-07-02T13:18:44.153 に答える
1

ルートに update メソッドが表示されません。ルートに追加するだけですroutes.rb

( の update メソッドの場合administrator_pages_controller.rb)

match '/recalls',  to: 'administrator_pages#recalls', :as => :recalls
match '/edit/:id', to: 'administrator_pages#edit', :as => :edit_recall
put '/update/:id', to: 'administrator_pages#update'm :as => :update_recall

そして実行するrake routesと、次のように表示されます

   recalls       GET    /recalls                       administrator_pages#recalls
   edit_recall   GET    /edit/:id(.:format)            administrator_pages#edit
   update_recall PUT    /update/:id(.:format)          administrator_pages#update

http://localhost:3000/recallsアクションを思い出す

http://localhost:3000/edit/:id編集アクション

http://localhost:3000/update/:id更新アクション

フォームの編集は次のようになります。

<%= form_for(@recall, :url => update_recall_path(@recall), :html => { :method => :put }) do |f| %>

:url => update_recall_path(@recall)コール更新アクションと使用:html => { :method => :put }

コントローラーの更新方法

def update
  @recall = Recall.find(params[:id])
      if @recall.update_attributes(params[:recall])
        # Handle a successful update.
        flash[:success] = "Recall updated"
        redirect_to recalls_path
      else
        render 'edit'
      end
end

recalls_path更新後はリダイレクトされますhttp://localhost:3000/recalls

あなたのコードのようにローカルホストで試してみましたが、うまくいきました。この助けを願っています。

Started PUT "/update/1" for 127.0.0.1 at 2013-07-02 20:37:14 +0700
Processing by AdministratorPagesController#update as HTML
  Parameters: {"utf8"=>"V", "authenticity_token"=>"s0tVbNt0JedecA+iCVlJ9GmIhGCsf
ltTbb1ep+mZmcY=", "recall"=>{"name"=>"test"}, "commit"=>"Update Recall", "id"=>"1"
}
  ←[1m←[36mRecall Load (0.0ms)←[0m  ←[1mSELECT "recalls".* FROM "recalls" WHERE "recalls"."id" = ? LIMIT 1←[0m  [["id", "1"]]
  ←[1m←[35m (0.0ms)←[0m  begin transaction
  ←[1m←[36m (1.0ms)←[0m  ←[1mUPDATE "recalls" SET "name" = 'test', "updated_at" =
 '2013-07-02 20:37:14.772915' WHERE "recalls"."id" = 1←[0m
  ←[1m←[35m (6.0ms)←[0m  commit transaction
Redirected to http://localhost:3000/recalls
Completed 302 Found in 13ms (ActiveRecord: 7.0ms)
于 2013-07-02T13:39:08.667 に答える
-1

次のコードを試してください

<%= form_for(@recall, :url => recall_path(@recall), :method => 'PUT') do |f| %>
于 2013-07-02T12:25:13.070 に答える