【问题标题】:ruby on rails myprofile pageruby on rails myprofile 页面
【发布时间】:2016-06-20 12:33:18
【问题描述】:

我想为注册的用户创建个人资料页面 所以假设一个用户已经注册,他的个人资料链接应该是

website.com/profile/username

并且在 views/profile/index.html.erb 我猜每个用户都应该看到自己的个人资料并使用 form_for 进行编辑

到目前为止,我有 profiles_controller.rbprofile.rb 模型和 resources :profilesfor my routes.rb

最好的方法是什么?

【问题讨论】:

    标签: ruby-on-rails routes slug


    【解决方案1】:

    如果你想通过用户名获取,那么你需要使用 slug。以下示例适用于id 版本。

    配置文件控制器

    def show
       @profile = Profile.find(params[:id])
    end
    

    路线:

    get 'profile/:id' => 'profile#show'
    

    对于:slug 版本,url 是这样的:http://localhost:3000/profiles/**username**

    路线:

    resources :profiles, param: :slug
    

    profiles_controller

    def show 
    end
    
    private
    
    def set_profile
      @profile = Profile.find_by_username(params[:slug])
    end
    

    或者你可以使用 gem 来处理这个https://github.com/norman/friendly_id

    【讨论】:

    • 我觉得你也应该为视图写一个例子?
    • @user4571629 你想让我做整页?此答案是针对您的问题。
    • @user4571629 另外,如果想在 twitter 中编辑选项,那么您可以通过 ajaxing 获得编辑表单。
    • 好的,谢谢,所以基本上我也是 param: :permalink ,我选择名字,对吧?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-19
    • 2023-03-03
    • 2014-10-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多