Tôi gặp lỗi với tuyến đường khi cố gắng ghi đè to_param trong mô hình người dùng của mình để sử dụng địa chỉ email làm id. Dường như cố gắng khớp toàn bộ đối tượng cho id khi nó cố khớp với tuyến đường. Bất cứ ai có thể giúp tôi tìm ra những gì tôi đang mất tích?Rails 3.0: lỗi với các tuyến đường khi ghi đè to_param trong mô hình
Dưới đây là các lỗi:
No route matches {:controller=>"users", :action=>"show", :id=>#<User id: 1, email: ....>}
Đây là cách tôi đã thiết lập mã.
mô hình/user.rb:
attr_accessible :email
def to_param
email
end
controllers/users_controller.rb:
before_filter :get_user, :only=>[:show,:update,:edit,:destroy]
...
def get_user
@user = User.find_by_email params[:id]
end
config/routes.rb
resources :users
Và đây là kết quả từ các tuyến đường cào:
user GET /users(.:format) {:controller=>"users", :action=>"index"}
POST /users(.:format) {:controller=>"users", :action=>"create"}
new_user GET /users/new(.:format) {:controller=>"users", :action=>"new"}
edit_user GET /users/:id/edit(.:format) {:controller=>"users", :action=>"edit"}
user GET /users/:id(.:format) {:controller=>"users", :action=>"show"}
PUT /users/:id(.:format) {:controller=>"users", :action=>"update"}
DELETE /users/:id(.:format) {:controller=>"users", :action=>"destroy"}
cho bạn thấy liên kết, dẫn đến lỗi này, xin vui lòng.Tất cả mã của bạn là đúng, do đó, lỗi nằm trong liên kết của bạn – fl00r
Sự cố đang hiển thị, chỉnh sửa, cập nhật và hủy đường dẫn. Vì vậy, hoặc cố gắng điều hướng đến đường dẫn trong trình duyệt (/users/[email protected] hoặc gọi liên kết_to trong chế độ xem, chẳng hạn như <% = link_to "Hiển thị", người dùng%> hoặc <% = link_to "Chỉnh sửa" , edit_user_path (user)%> – Bee