我有三个班级:
class Valoration < ActiveRecord::Base
validates :points, presence:true, numericality: { only_integer: true, greater_than_or_equal_to: 0, less_than_or_equal_to: 100 }
belongs_to :user
belongs_to :hability
end
#################################################################
class Hability < ActiveRecord::Base
validates :name, presence: true, length: { minimum: 4, maximum: 16 }
has_many :valorations
end
#################################################################
class User < ActiveRecord::Base
validates :password, presence: true, confirmation: {strict: true}
#validates :password_confirmation, presence: true
validates :telephone, uniqueness: true, presence: true, numericality: { only_integer: true }, presence: true, length: { minimum: 9, maximum: 9 }
validates :name, presence: true, length: { minimum: 4, maximum: 30 }, format: { with: /^[\w\s-]*/u, multiline: true,
message: 'only allows letters' }
has_many :valorations
end
这是db方案,让事情清楚
ActiveRecord::Schema.define(version: 20150709141437) do
create_table "habilities", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "users", force: :cascade do |t|
t.string "name"
t.string "password"
t.integer "telephone"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "valorations", force: :cascade do |t|
t.integer "user_id"
t.integer "hability_id"
t.integer "points"
t.string "date"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "valorations", ["hability_id"], name: "index_valorations_on_hability_id"
add_index "valorations", ["user_id"], name: "index_valorations_on_user_id"
end
Valorations是用户的资源:
resources :users do
resources :valorations
end
在users / show.html.erb中,我有以下链接:
<%= link_to 'View valorations on this user', user_valoration_path%>
<%= link_to 'Valorate user:',new_user_valoration_path(@user)%>
但是当我加载页面时,它给了我这个错误:
Showing /home/manuel/Documentos/ComoMeVeis/app/views/users/show.html.erb where line #11 raised:
No route matches {:action=>"show", :controller=>"valorations", :id=>"2"} missing required keys: [:user_id]
我认为表关联有问题,因为它说:valorations:id = 2,但是它说id = 2 我是rails的新手,我不知道如何解决这个问题,如果您需要有关代码的更多信息,请发表评论,欢迎任何帮助!
答案 0 :(得分:3)
以下路线
resources :users do
resources :valorations
end
产生
GET /users/:user_id/valorations/:id
GET /users/:user_id/valorations
和其他一些与此答案无关的
问题出在link_to
<%= link_to 'View valorations on this user', user_valoration_path%>
user_valoration_path
会尝试显示一个用户值,这意味着您需要显示user
和valoration
。
我从链接文本中假设您要为用户显示所有的值,因此您需要使用以下
user_valorations_path(@user)
这将指向路径/users/1/valorations
而不是/users/1/valorations/...