2013-06-04 131 views
-1

我想重命名我的Rails应用程序的电子邮件输入的路由。目前,当有人输入他们的电子邮件时,它将它们路由到/:controller /:id 我想这样做,使得一旦他们输入了他们的电子邮件,它就不会显示他们的ID,因此他们不能更改ID号并查看其他人的电子邮件。Rails重命名路由

+0

“当有人输入自己的电子邮件” - >输入在哪里?你为什么不简单地添加'before_filter'来限制它。 – kiddorails

回答

0

编辑:

我是想我的自我,这不到风度如果电子邮件的作品,但如果你有一个用户名,没有特殊carracters尝试它会正常工作!

尝试这样:

路线:

match "/controller/:username" => "controller#show" 

控制器:

def show 
    @user = find_by_username(params[:username]) 
end 
+0

我试过了,但是当他们输入他们的电子邮件时,他们被路由到/ emailinputs /:id –

0

我想,用一些字母一个固定链接从另一个给安全电子邮件。假设你有人的模型。

你只需要一个属性添加到people,属性名是permalink

尝试运行

rails g migration add_permalink_to_peoples permalink:string 
rake db:migrate 

people.rb,玩before_save

class People < ActiveRecord::Base 
    attr_accessible :email, :permalink 
    before_save :make_it_permalink 

def make_it_permalink 
    # this can create permalink with random 8 digit alphanumeric 
    self.permalink = SecureRandom.base64(8) 
end 

end 

而且你routes.rb

match "/people/:permalink" => 'peoples#show', :as => "show_people" 

peoples_controller.rb

def show 
    @people = People.find_by_permalink(params[:permalink]) 
end 

运行rails server,并添加一个人。

人与随机8位字母数字的唯一永久链接(例如/people/:permalink

例如:http://localhost:3000/people/9sd98asj