Question

I have a RESTful controller inside of a namespace called dashboard, so my URL looks like this:

  • /dashboard/member
  • /dashboard/member/edit

Something weird is happening when I submit the member form with a validation error... it shows the error like it's suppose to, but when it goes to the PATCH url "/dashboard/member" it comes with an ".2" in the end:

  • /dashboard/member.2

the "2" is the ID of the record.

The funny thing is that I did everything correctly and it works great, this ".2" is the only thing that is bothering my head.

My Controller

class Dashboard::MembersController < ApplicationController

  load_and_authorize_resource :class => Member
  before_filter :authenticate_member!

  def show
  end

  def edit
    @member ||= current_member
  end

  def update
    @member ||= current_member
    if @member.update_attributes(member_params)
      flash[:success] = "Profile updated"
      redirect_to dashboard_member_path
    else
      render "edit"
    end
  end

  private 

  def member_params
    params.require(:member).permit(:first_name, :last_name, :address, :city, :state, :country, :zipcode, :home_phone, :cell_phone)
  end

end

My Route

namespace :dashboard do 
  resource :member, only: [:show, :edit, :update]
end
Was it helpful?

Solution

If you meant to use resource :member (instead of resources :member) then you should know that it always looks up without referencing an ID.

Here is how the routes would be created without id's:

 edit_dashboard_member        GET    /dashboard/member/edit(.:format)                              dashboard/members#edit
             dashboard_member GET    /dashboard/member(.:format)                                   dashboard/members#show
                              PATCH  /dashboard/member(.:format)                                   dashboard/members#update
                              PUT    /dashboard/member(.:format)                                   dashboard/members#update

When you are sending a PATCH request make sure that you don't pass an argument with it. If you pass an argument then it would be interpreted as format(like .html, .js etc). In your case you passed an argument as 2 or a member with an id 2

For eg:

PATCH request to dashboard_member_path(2)

The route was matched against PATCH /dashboard/member(.:format) dashboard/members#update

2 is interpreted as (.:format) because there is no :id part.

OTHER TIPS

I had to remove the @member from my form, so... I had this:

<%= form_for(@member, url: dashboard_member_path(@member), html: {method: "patch", class: "form-horizontal"}) do |f| %>

And it became this:

<%= form_for(@member, url: dashboard_member_path, html: {method: "patch", class: "form-horizontal"}) do |f| %>

Now, there's IDs being passed to the URL.

Thanks guys!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top