Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 4 将rails中的post/:id更改为显示post/:restname_Ruby On Rails 4_Friendly Id - Fatal编程技术网

Ruby on rails 4 将rails中的post/:id更改为显示post/:restname

Ruby on rails 4 将rails中的post/:id更改为显示post/:restname,ruby-on-rails-4,friendly-id,Ruby On Rails 4,Friendly Id,我对Rails还是新手,我对路线有一个问题 现在,我有来自post表的资源 post_path GET /posts/:id(.:format) posts#show PATCH /posts/:id(.:format) posts#update PUT /posts/:id(.:format) posts#update DELETE

我对Rails还是新手,我对路线有一个问题

现在,我有来自post表的资源

post_path   GET             /posts/:id(.:format)    posts#show
            PATCH           /posts/:id(.:format)    posts#update
            PUT             /posts/:id(.:format)    posts#update
            DELETE          /posts/:id(.:format)    posts#destroy
当我点击show页面,而不是地址栏上说:

http://localhost:3000/posts/3 
我可以说:

http://localhost:3000/posts/restaurantName  ? 
:restname是用户中具有餐厅名称的列

我正在为餐厅员工构建一个web应用程序,我希望它的地址以餐厅老板的餐厅名称结尾,特别是Post.user.restname


谢谢你的帮助

我的声誉不够高,所以我不能同时喜欢你的评论,但如果可以的话,我会喜欢的

由于我的表格的设置方式,我需要的信息在用户下,我希望用用户信息更改我帖子下的地址。最后,我能得到的最接近的方法是使用参数化

在我的帖子中:

45   @post = Post.new(post_params)
46   if @post.save 
47     @post.update :postname => @post.user.restname
48     redirect_to @post
49   else
在我的帖子模型中:

13   def to_param
14     "#{id} #{postname}".parameterize
15   end

谢谢,这里的两条评论真的帮助我理解了如何实现这一点。但有一个独特的问题。我有一个Post模型,我在Post show中输出Post。如何使用post address中User表下的:restname值?可能吗?谢谢