Saghir A. Khatri
Saghir A. Khatri

Reputation: 3128

Edit/Update calls the create in ruby on rails

I am stuck in weird problem. I have few recall values that are created and deleted correctly, but when i try to edit the values and save them, it created new recall.

here is my controller for Edit/update

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

my edit.html.erb is as follows

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

please let me know where i did wrong. i tried to define :action => 'edit' but it didn't worked out. Thanks in advance

EDIT rake routes output is here

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

here is my routes.rb, looking at rake routes and my routes.rb i can see something wrong. but unable to firgure out the problem

 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"

Upvotes: 0

Views: 1456

Answers (3)

rails_id
rails_id

Reputation: 8220

I'm not seeing an update method on your routes, just add to your routes.rb

(if an update method on 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

And run rake routes and you will see looks like

   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 recalls action

http://localhost:3000/edit/:id edit action

http://localhost:3000/update/:id update action

Your form edit looks like :

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

:url => update_recall_path(@recall) for call update action and using :html => { :method => :put }

Your controller update method

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 is after update will redirect into http://localhost:3000/recalls

I have try it on my localhost like your code and it's works. Hope this help.

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)

Upvotes: 1

rmagnum2002
rmagnum2002

Reputation: 11421

you need to send an id of a recallwithin your route, because in edit/update actions you do:

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

your route for edit should look like this: match 'edit/:id', to: 'administrator_pages#edit', as: 'edit_recall' and looks like you'll need one more for update but with method: :put

with the above route you'll have a url like this:

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

but if you want administrator_pages ahead you'll have to modify your routes:

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

result:

localhost:3000/administrator_pages/recall/3/edit

at the request params of id will be sent and you can use that Recall.find(params[:id]) in your controller. And you'll have to draw a route for update action too with method put

A better solution, I would add resource recalls to routes:

resources :recalls

this would give me all needed routes to work with recall, edit, new, show, etc..

Upvotes: 1

Bachan Smruty
Bachan Smruty

Reputation: 5734

Try the following code

<%= form_for(@recall, :url => recall_path(@recall), :method => 'PUT') do |f| %>

Upvotes: -1

Related Questions