Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.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 RubyonRails在搭建之后添加新页面_Ruby On Rails_Scaffold - Fatal编程技术网

Ruby on rails RubyonRails在搭建之后添加新页面

Ruby on rails RubyonRails在搭建之后添加新页面,ruby-on-rails,scaffold,Ruby On Rails,Scaffold,我制作了一个名为b_page的脚手架,为bio创建了一个迁移,并添加了一个biopage.html.erb 在控制器中: def biopage @b_pages = BPage.all end 在routes.rb中 resources :b_pages do collection do get 'biopage' end end 在bio.html.erb中: <div class="jumbotron"> <h1>Bio of :</

我制作了一个名为b_page的脚手架,为bio创建了一个迁移,并添加了一个biopage.html.erb 在控制器中:

def biopage
@b_pages = BPage.all
end
在routes.rb中

resources :b_pages do  
collection do  
  get 'biopage'  
 end 
end 
在bio.html.erb中:

<div class="jumbotron">
<h1>Bio of :</h1>
<h2><b><%= @b_page.Bpage_name %></b></h2>
<h3><%= @b_page.user.email %></h3>
</div>
<%= @b_page.bio %>

首先,我觉得这有点奇怪:

resources :b_pages do  
  collection do  
    get 'biopage'  
  end 
end
因为它会产生一个类似于:
/b_pages/biopage
的路由。您可能只想执行以下操作:

resources :b_pages, except: :show
get '/biopage/:id', to: 'b_pages#show'
这样,您的
biopage
路线将转到
show
controller方法,您仍然可以使用其他
b_页面
路线

您将看到
ActiveRecord::RecordNotFound
错误消息,因为您没有要显示的
BPage
对象,因此
show
方法正在抱怨。请注意我上面写的路由如何使用
:id
——这是因为
show
操作通常会在前端显示某个记录的id。如果要使用
biopage
并将其链接到
show
方法,则应返回要实际显示的对象。否则,您可能应该为
biopage
创建一个完全不同的控制器方法,该方法不会干扰
b_页面
资源。在路线上类似这样:

resources :b_pages
get '/biopage/:id', to: 'b_pages#your_method'
在控制器里,你会有

class BPages < ApplicationController
  # index, show, destroy, etc. here

  def your_method
    # Get whatever object you want returned here
  end
end
class BPages
您的控制器中有@b_页面,并且正在试图访问视图中的@biopage。此外,如果使用
all
,则必须循环使用它。请把问题弄清楚。您正在使用参数中的id键绑定
@b_page
,但您没有将路由设置为在url中查找。尝试将其更改为
get'biopage:id'
class BPages < ApplicationController
  # index, show, destroy, etc. here

  def your_method
    # Get whatever object you want returned here
  end
end