2012-11-23 63 views
1

我有一个Ruby on Rails应用程序,您可以在其中创建'posts'。我开始使用脚手架生成器来生成标题,这是一个字符串,而内容是主体。在Ruby on Rails中更改参数[:id]

每个“交”具有ID的URL,例如,/ 1,/ 2,/ 3等

是否有一种方法来改变它的generater随机字符和数字的字符串,用于例如/ 49slc8sd,/ l9scs8dl等?

以下是我对posts_controller.rb

class PostsController < ApplicationController 
    # GET /posts 
    # GET /posts.json 
    def index 
    @posts = Post.all 

    respond_to do |format| 
     format.html # index.html.erb 
     format.json { render json: @posts } 
    end 
    end 

    # GET /posts/1 
    # GET /posts/1.json 
    def show 
    @post = Post.find(params[:id]) 

    respond_to do |format| 
     format.html # show.html.erb 
     format.json { render json: @post } 
    end 
    end 

    # GET /posts/new 
    # GET /posts/new.json 
    def new 
    @post = Post.new 

    respond_to do |format| 
     format.html # new.html.erb 
     format.json { render json: @post } 
    end 
    end 

    # GET /posts/1/edit 
    def edit 
    @post = Post.find(params[:id]) 
    end 

    # POST /posts 
    # POST /posts.json 
    def create 
    @post = Post.new(params[:post]) 

    respond_to do |format| 
     if @post.save 
     format.html { redirect_to @post, notice: 'Post was successfully created.' } 
     format.json { render json: @post, status: :created, location: @post } 
     else 
     format.html { render action: "new" } 
     format.json { render json: @post.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # PUT /posts/1 
    # PUT /posts/1.json 
    def update 
    @post = Post.find(params[:id]) 

    respond_to do |format| 
     if @post.update_attributes(params[:post]) 
     format.html { redirect_to @post, notice: 'Post was successfully updated.' } 
     format.json { head :no_content } 
     else 
     format.html { render action: "edit" } 
     format.json { render json: @post.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # DELETE /posts/1 
    # DELETE /posts/1.json 
    def destroy 
    @post = Post.find(params[:id]) 
    @post.destroy 

    respond_to do |format| 
     format.html { redirect_to posts_url } 
     format.json { head :no_content } 
    end 
    end 
end 

这里是我在post.rb模型

class Document < ActiveRecord::Base 
    attr_accessible :content, :name 
end 
+1

您能否重新解释一下这个问题?我不确定你在问什么。这可能意味着六十件事情(我可以​​从头顶开始思考)。 – omninonsense

+0

我已经为你改写过。希望它更有意义。 – user1658756

回答

0

目前还不清楚你问这里。路由中指定的动作路径不要求传递的ID具有特定的格式。你可以传递非数字的ID,如果你想和你的动作一样使用你想要的ID。也许如果你提供了关于路线和行动的更多信息,我们可以理解你所要求的。

+0

一点都不清楚(他在问什么) – Agush

+0

我已经为你改写了它。希望它更有意义。 – user1658756

1

如果你希望你的模型不要有其主键ID可预测的顺序,你可以但是你也可以根据航线上的任何基于uuid或用东西帮助像http://codesnipers.com/?q=using-uuid-guid-as-primary-key-in-rails

的GUID ID唯一标识这是推荐的方法,如果万一你不想暴露数据库标识符在你的路由

person/:person_random_token, :controller => :persons, :action => :show #adding this in your route file directing to the controller where you can use params[:person_random_token] to uniquely identify your person object in Persons model 

在你的控制器的操作的资源的其他财产,你可以说

Person.find_by_random_token(params[:person_random_token]) #assuming random_token is your column name 

得到Person对象

0

还有a number of ways如何在Ruby中生成随机字符串。

现在,到您的问题的第二部分。如果你想要使用像/post/rndm5tr的路线来访问你的帖子,你可以简单地改变这行代码的控制器内:

@post = Post.find(params[:id]) 

@post = Post.find_by_randomness(params[:id]) 

现在,只需创建一个迁移:rails g migration AddRandomnessToPost randomness:string和运行rake db:migrate(或bundle exec rake db:migrate,取决于它的设置)。

当然,您可以随意命名该字段,randomness只是我使用的随机名称。我认为常见的惯例是称它们为slu or或代币,但我可能是错的。

现在,在你的模型中添加一个方法before_create生成随机字符串,并将其添加到即将被保存的Post对象(使用从上面的链接一个例子)。检查你生成的字符串是否已经被使用是明智的(你可以编写一个递归方法,如果一个帖子已经有随机标记,它会再次调用它自己)。

1

您还应该知道ActiveRecord :: Base对象的to_param方法。

基本上,Rails在你的对象上调用这个方法来知道要在URL和params [:id]中放置什么。默认情况下,它只是数据库中记录的主键。假设你覆盖它:

class Post < ActiveRecord::Base 

    def to_param 
    return id*100 
    end 

    def self.find_by_new_id(n) 
    return self.find(n/100) # really you'd want to handle strings and integers 
    end 
end 

数据库中的第一条记录将有url /posts/100

在你的控制器,以获取你只是做对象

@post = Post.find_by_new_id(params[:id]) 

(当然你可以覆盖默认find方法为好,但可能是令人难以接受的。)基本上to_param方法将你的ID新的发现者将其解除。通常,您只需指向创建记录时通过钩子自动填充的另一个数据库列。这是Qumara otBurgas发布的链接中描述的内容。