SeekingTruth
SeekingTruth

Reputation: 1054

post to an update action with out an id in rails 4.1.x

I have a controller:

module Xaaron class PermissionsManagementController < ApplicationController end end

chick has create, edit, update, new and destroy.

edit and destroy I was able to do something like:

  get 'edit_group_membership' => 'permissions_management#edit', :as => 'edit_group_membership'
  get 'remove_group_membership' => 'permissions_management#destroy', :as => 'remove_group_membership'

which allows me to by pass passing in a id. There is no model object for this controller so there is no need for an id. but my question is, how do i post to the update action in rspec and in the form_tag with out passing in an id? and with out it exploding stating that I am missing an id.

in the spec can I do: post :update, id: '', {}? What would I do for form_tag?

Upvotes: 1

Views: 69

Answers (1)

rossta
rossta

Reputation: 11494

You could start with something like this:

# routes.rb
post 'permissions_management' => 'permissions_management#update', as: 'change_permissions_management'

# view
form_tag(change_permissions_management_path)

If it were my app, I would treat this as a singular resource, for which Rails provides out-of-the-box support. As you can see in the rake routes output, the :id param is not required for the show, update and destroy routes.

# routes.rb
resource :permissions_management # note the singular "resource"

# view
form_tag(permissions_management_path)

# $ rake routes | grep permissions_management
     permissions_management POST   /permissions_management(.:format)                               permissions_managements#create
 new_permissions_management GET    /permissions_management/new(.:format)                           permissions_managements#new
edit_permissions_management GET    /permissions_management/edit(.:format)                          permissions_managements#edit
                            GET    /permissions_management(.:format)                               permissions_managements#show
                            PATCH  /permissions_management(.:format)                               permissions_managements#update
                            PUT    /permissions_management(.:format)                               permissions_managements#update
                            DELETE /permissions_management(.:format)                               permissions_managements#destroy

Upvotes: 1

Related Questions