我正在使用Rails 4。
我想强迫游客选择他的国家。拉脱维亚和爱沙尼亚只有两种选择。
在主要布局中,我喜欢这样:
<li><%= link_to "Latvija", :country => :latvia %></li>
<li><%= link_to "Igaunija", :country => :estonia %></li>
在路线中:
Rails.application.routes.draw do
scope "(:country)", :locale => /latvia|estonia/ do
resources :reports
devise_for :admin_users, ActiveAdmin::Devise.config
ActiveAdmin.routes(self)
#get "/accept" => 'home#accept_and_redirect'
scope "(:locale)", :locale => /lv|ee|ru/ do
root 'home#index'
get 'advertisements/update_regions' => 'advertisements#update_regions', as: 'update_regions'
resources :reviews do
member do
put "like", to: "reviews#upvote"
put "dislike", to: "reviews#downvote"
end
end
resources :advertisements do
collection { post :search, to: 'advertisements#index' }
member do
put "like", to: "advertisements#upvote"
put "dislike", to: "advertisements#downvote"
end
end
resources :services
get "/help", to: "help#index"
get "/rules", to: "rules#index"
get "/blacklist", to: "blacklist#index"
devise_for :users
resources :users, only: [:show, :index] do
member do
put "like", to: "users#upvote"
put "dislike", to: "users#downvote"
end
end
end
end
end
它基本上做了什么:它将网址更改为http://localhost:3000/latvia/lv/users/sign_up
(有效)
但是当我去http://localhost:3000/latvia/lv/advertisements
时,它给了我错误:
No route matches {:action=>"show", :controller=>"advertisements", :country=>#<Advertisement id: 82, name: "tij", region_id: 4, age: 19, height: 180, phone_number: "0202222", weight: 85, email: "as@g.lv", description: "ahahi", created_at: "2014-11-28 14:39:12", updated_at: "2014-11-28 14:40:14", user_id: 14, country_id: 1, in_blacklist: false, admin_confirmed: false, vip: true, provaider: "few", your_ip: "wefwef">, :format=>nil, :id=>nil, :locale=>:lv} missing required keys: [:id]
因此,在scope "(:country)", :locale => /latvia|estonia/ do end
之外声明的链接有效,但内部的链接无效。
什么可能导致此错误?或者是在链接中设置国家/地区的其他方式?
由于
答案 0 :(得分:0)
你只是错误地构建了预期的参数 - 或者没有传递所需的一切。看看:
No route matches {
:action=>"show",
:controller=>"advertisements",
:country=>#<Advertisement id: 82,
name: "tij",
region_id: 4,
age: 19,
height: 180,
phone_number: "0202222",
weight: 85,
email: "as@g.lv",
description: "ahahi",
created_at: "2014-11-28 14:39:12",
updated_at: "2014-11-28 14:40:14",
user_id: 14,
country_id: 1,
in_blacklist: false,
admin_confirmed: false,
vip: true,
provaider: "few",
your_ip: "wefwef">,
:format=>nil,
:id=>nil,
:locale=>:lv
}
missing required keys: [:id]
似乎:country
实际上是Advertisement
类的一个实例 - 对我来说真的没有意义,所以这可能是一个错误。可能你的范围正在弄乱另一个参数,因为它们具有相同的名称。
更重要的是,由于:id
为零,因此您需要将ID传递给linked_to
来电。像这样:
link_to "Latvija", :country => :latvia, :id => <whatever you want this id to be>