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',但没有成功。提前致谢

编辑 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,查看 rake 路线和我的 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

我没有在您的路线上看到更新方法,只需添加到您的routes.rb

(如果更新方法在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

您需要在您的路线中发送一个 id recall,因为在编辑/更新操作中您执行以下操作:

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

您的编辑路线应如下所示: match 'edit/:id', to: 'administrator_pages#edit', as: 'edit_recall' 并且看起来您还需要一个更新但method: :put

使用上述路线,您将拥有这样的网址:

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

试试下面的代码

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