ruby on rails - unknown action in controller - routes.rb issue? weird results by changing from singular to plural? -
i'm getting strange reaction 'destroy' method. error when trying destroy project:
unknown action action '5' not found projectscontroller
i figured out when when change routes.rb file resources :projects
(plural) resources :project
(singular), destroy action works should (and returns index) show method works update , new methods throw undefined method
projects_path'` errors. why happening??
class projectscontroller < applicationcontroller def index @projects = project.sorted end def show @project = project.find(params[:id]) end def new @project = project.new @project_count = project.count + 1 end def create # instantiate new object using form parameters @project = project.new(project_params) # save object if @project.save # if save succeeds, redirect index action flash[:notice] = "project created successfully." redirect_to(:action => 'index') else # if save fails, redisplay form user can fix problems @project_count = project.count + 1 render('new') end end def edit @project = project.find(params[:id]) @project_count = project.count end def update @project = project.find(params[:id]) if @project.update_attributes(project_params) flash[:notice] = "project updated successfully." redirect_to(:action => 'show', :id => @project.id) else @project_count = project.count render('edit') end end def delete @project = project.find(params[:id]) end def destroy project = project.find(params[:id]) project.destroy flash[:notice] = "project '#{project.name}' destroyed successfully." redirect_to(:action => 'index') end private def project_params params.require(:project).permit(:name, :financing, :visible, :position) end end
routes.rb file:
rails.application.routes.draw root :to => 'projects#index' resources :projects match ':controller(/:action(/:id))', :via => [:get, :post] # priority based upon order of creation: first created -> highest priority. # see how routes lay out "rake routes". # can have root of site routed "root" # root 'welcome#index' # example of regular route: # 'products/:id' => 'catalog#view' # example of named route can invoked purchase_url(id: product.id) # 'products/:id/purchase' => 'catalog#purchase', as: :purchase # exampl e resource route (maps http verbs controller actions automatically): # resources :products # example resource route options: # resources :products # member # 'short' # post 'toggle' # end # # collection # 'sold' # end # end # example resource route sub-resources: # resources :products # resources :comments, :sales # resource :seller # end # example resource route more complex sub-resources: # resources :products # resources :comments # resources :sales # 'recent', on: :collection # end # end # example resource route concerns: # concern :toggleable # post 'toggle' # end # resources :posts, concerns: :toggleable # resources :photos, concerns: :toggleable # example resource route within namespace: # namespace :admin # # directs /admin/products/* admin::productscontroller # # (app/controllers/admin/products_controller.rb) # resources :products # end end
delete.html.erb page:
<% @page_title = "delete project" %> <%= link_to("<< list", {:action => 'index'}, :class => 'back-link') %> <div class="project destroy"> <h2>delete projects</h2> <%= form_for(:project, :url => {:action => 'destroy', :id => @project.id}) |f| %> <p>are sure want permanently delete project?</p> <p class="reference-name"><%= @project.name %></p> <div class="form-buttons"> <%= submit_tag("delete project") %> </div> <% end %> </div>
[update] interesting update. messed around destroy , changed controller to:
def delete @project = project.find(params[:id]) @project.destroy end def destroy project = project.find(params[:id]) project.destroy flash[:notice] = "project '#{project.name}' destroyed successfully." redirect_to(:action => 'index') end
i did see if issue destroy method. destroy project, , still returns delete page causes same error, destroy it.
though doing couple of unconventional stuff having delete.html.erb
(objects deleted show view page), i'm going address direct question.
you said:
"when change routes.rb file resources :projects (plural) resources :project (singular), destroy action works should (and returns index) show method works update , new methods throw undefined method projects_path'` errors. why happening??"
you have understand every time change routes, have modify helper paths in views.
example: when had resources :projects
, helper methods (run rake db:migrate)
projects_path /projects(.:format) projects#index post /projects(.:format) projects#create new_project_path /projects/new(.:format) projects#new edit_project_path /projects/:id/edit(.:format) projects#edit project_path /projects/:id(.:format) projects#show patch /projects/:id(.:format) projects#update put /projects/:id(.:format) projects#update delete /projects/:id(.:format) projects#destroy
so @ point, projects_path
working because can see above, projects_path
there.
when changed routes resources :project
(singular), helper paths change well. run rake db:migrate
see them:
project_index_path /project(.:format) project#index post /project(.:format) project#create /project/new(.:format) project#new /project/:id/edit(.:format) project#edit /project/:id(.:format) project#show patch /project/:id(.:format) project#update put /project/:id(.:format) project#update delete /project/:id(.:format) project#destroy
as can see helper path maps project#index
project_index_path
.
so i'm guessing in edit.html.erb
(or somewhere else) have old code: projects_path
, getting undefined method projects_path
because helper no longer exist. correct helper method in case, project_index_path
.
Comments
Post a Comment