我正在制作一个带有一个控制器的网站" Projects"我希望将所有项目显示到路线:
我在route.rb中尝试了这个
get 'front/:id' => 'projects#show', :constraints => { :id => /[^/]+/ },但它不起作用。
我的档案:
的routes.rb
Rails.application.routes.draw do resources :users, path: '/admin/clients' get 'admin' => 'admin#dashbord' get 'admin/profile' get 'admin/settings' get 'admin/_admin_header' get 'front' => 'front#index' get 'front/profile' => 'front#profile' get 'front/:id' => 'projects#show' scope '/admin' do resources :projects do resources :pictures end end end
projects_controller.rb
layout 'adminApplication' before_action :set_project, only: [:show, :edit, :update, :destroy] def index @projects = Project.all end def show end def new @project = Project.new end def edit end def create @project = Project.new(project_params) respond_to do |format| if @project.save format.html { redirect_to @project, notice: 'Project was successfully created.' } format.json { render :show, status: :created, location: @project } else format.html { render :new } format.json { render json: @project.errors, status: :unprocessable_entity } end end end def update respond_to do |format| if @project.update(project_params) format.html { redirect_to @project, notice: 'Project was successfully updated.' } format.json { render :show, status: :ok, location: @project } else format.html { render :edit } format.json { render json: @project.errors, status: :unprocessable_entity } end end end def destroy @project.destroy respond_to do |format| format.html { redirect_to projects_url, notice: 'Project was successfully destroyed.' } format.json { head :no_content } end end private def set_project @project = Project.find(params[:id]) end def project_params params.require(:project).permit(:name, :date, :location, :integer) end end
front_controller.rb
def index @projects = Project.all render 'projects/index' end def show end def profile end end
in projects / index.html.erb
- link_to 'Show', project - link_to 'Show', front_path(project)
我已经检查了所有类似的问题。
感谢您的帮助!
Kazei Design
更新
$out = [];
foreach($trainers as $trainerId=> $trainerName){
$out[] = array(
'id'=>$trainerId,
'name'=>$trainerName,
'gold'=>isset($gold[$trainerId])?$gold[$trainerId]:0,
'silver'=>isset($silver[$trainerId])?$silver[$trainerId]:0,
'bronze'=>isset($bronze[$trainerId])?$bronze[$trainerId]:0,
);
}
uasort($out, function($a, $b){
// Here: sort by your algorithm. Here is example:
if($a['gold'] != $b['gold']){
return $b['gold'] - $a['gold'];
}
if($a['silver'] != $b['silver']){
return $b['silver'] - $a['silver'];
}
return $b['bronze'] - $a['bronze'];
});
$placeId = 1;
foreach($out as &$info){
$info['place'] = $placeId++;
}
unset($info);
foreach($out as $info){
echo "{$info['place']} place goes to - {$info['name']} ({$info['id']}) as he/she got {$info['gold']} gold medals, {$info['silver']} silver and {$info['bronze']} bronze";
}
:
rake routes | grep front
答案 0 :(得分:2)
您正在使用命名路线助手,但您没有指定它:
front_path
您可以在自己的路线中看到projects#show
rake routes | grep front
front GET /front(.:format) front#index
front_profile GET /front/profile(.:format) front#profile
GET /front/:id(.:format) projects#show
不存在:
routes.rb
所以,在你的get 'front/:id' => 'projects#show', as: :custom_front
添加助手:
rake routes
现在运行custom_front_path
以查看新帮助程序(它应该是- link_to 'Show', custom_front_path(project)
)并使用它:
Log.i("GPSTracker GPS","lat : " + latitude + ", long : " + longitude);
Log.i("GPSTracker Net","lat : " + latitude + ", long : " + longitude);
中的更多信息
答案 1 :(得分:1)
您可能希望将路线更改为
get 'front.:id' => 'projects#show'