2014-02-26 36 views
0

我在配置由我的Rails 4應用程序中的腳手架生成的模型時遇到問題。如何在導軌中配置腳手架生成器的路徑

這是我的模型:

class Contact < ActiveRecord::Base 
    belongs_to :user 
end 

class User < ActiveRecord::Base 
    has_one :contact 

    after_create :make_contact 
    def make_contact 
     create_contact(
     :country => "USA", 
     :city => "Newyork" 
    ) 
    end 
end 

正如你所看到的,我創建一個剖面模型的實例爲每個用戶在註冊的網站。

我用Devise GEM生成用戶模型,並使用rails scaffold generator生成Contact模型。

1)我希望我的用戶只更新或查看他們的個人資料。我想阻止他們列出所有配置文件,銷燬他們的配置文件或創建一個新的配置文件。什麼是最好的方法來做到這一點?

2)我希望我的應用程序在訪問/聯繫路由時自動重定向到用戶相關的配置文件頁面。

3)用戶可以不能夠通過更改URL,看看其他用戶的配置文件如/聯繫人/ 1,聯繫人/ 2等

我怎樣才能做到這一點?

謝謝。在您的控制器

+0

什麼你問是你爲什麼要使用Devise。 – wurde

回答

1

用戶before_filter/before_action

def UsersController < ApplicationController 
before_filter :restrict_user, :only => [:show, :edit, :update] 

private 
def restrict_user 
    redirect_to :root, :alert => "Not authorized" unless params[:id] = current_user.id 
end 
end 

在你的路線,你可以指定只想要

resources :users, :only => [:new, :create, :edit, :update, :show] #index and destroy are not in the list 

的動作你可以做同樣的觸點控制太

+0

非常感謝!它運作良好。 – msdundar

+0

請選擇正確的答案 – usha

相關問題