Marcus
Marcus

Reputation: 9492

Rails add route for additional method action

I have read some other questions and answers on this topic, yet I'm an still really confused.

I have a user model with the standard functions. I want to add a page similar to edit/update that allows the user to connect with APIs.

I built the following functions into my controller:

def deviceConnectEdit
  @user = User.find(params[:id])
end

def deviceConnectUpdate
  @user = User.find(params[:id])
  if @user.deviceConnectUpdate_attributes(user_params)
    flash[:success] = "Device Connected"
    redirect_to @user
  else
    render 'deviceConnect'
  end
end

Very similar to the standard edit/update

I also built two files deviceConnectUpdate.html.erb and deviceConnectUpdate.html.erb (they have a

statement currently to see when they are called.

now when I run 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

when I call foobar/users/1/deviceConnectEdit I receive the error that there are not matching routes.

How do I fix this and add routes. Thanks

Upvotes: 1

Views: 1203

Answers (1)

Helios de Guerra
Helios de Guerra

Reputation: 3475

You still need to declare the routes in your config\routes.rb file.

Also, if you want to use more idiomatic ruby (which you do), you should be using underscored method names, not camelCase. e.g. device_connect_edit NOT deviceConnectEdit. You'll run into headaches down the road if you don't conform to the convention (unless you have a very good reason for departing from it). In that light I'll post the routes in idiomatic Ruby and hope you update your method names :-)

resources :users do
  member do
    get 'device_connect_edit'
    post 'device_connect_update'
  end
end

Also, you might want to strongly consider your design to have a separate device resource instead of tacking it on in the UsersController...

Upvotes: 1

Related Questions