Rails 更新模型不起作用:没有路由匹配,缺少必需的键:[:id]
Rails update model wont work : No route matches, missing required keys: [:id]
我刚刚开始我的第二个 Rails 项目,但在一些看起来很基本的事情上遇到了一些麻烦。不过我完全糊涂了。
我设置了两个模型,用户和配置文件。个人资料 belongs_to 用户和用户 has_one 个人资料。
我现在正在为配置文件设置编辑表单,无法通过此操作。编辑表单正确加载并将数据插入数据库但是当我提交表单时出现以下错误
No route matches {:action=>"show", :controller=>"profiles", :format=>nil, :id=>#<Profile id: 7, user_id: 7, content: "Fill this in", created_at: "2015-01-08 19:08:22", updated_at: "2015-01-08 19:08:22", title: "fooobar", department: "one bar", school: "dsadsa", education: "two bar", academic_focus: "my bar is broken", website: "who bar is bokrbne">} missing required keys: [:id]
我的模型是这样的
Class User < ActiveRecord::Base
has_one :profile, :class_name => 'Profile', :foreign_key => 'user_id'
attr_accessor :remember_token
end
class Profile < ActiveRecord::Base
belongs_to :user, :class_name => "User", :foreign_key => 'user_id'
attr_accessor :id
validates :content, presence: true, length: {maximum: 5000}
validates :user_id, presence: true
end
这是表格。我尝试了多种方法来添加 url: 参数到 form_for 但没有成功。
<%= form_for @profile do |f| %>
<%= f.label :title, class: 'col-lg-3 control-label' %>
<%= f.text_field :title, class: 'form-control' %>
<%= f.label :department, class: 'col-lg-3 control-label' %>
<%= f.text_field :department, class: 'form-control' %>
<%= f.label :school, class: 'col-lg-3 control-label' %>
<%= f.text_field :school, class: 'form-control' %>
<%= f.label :education, class: 'col-lg-3 control-label' %>
<%= f.text_field :education, class: 'form-control' %>
<%= f.label :academic_focus, class: 'col-lg-3 control-label' %>
<%= f.text_field :academic_focus, class: 'form-control' %>
<%= f.label :website, class: 'col-lg-3 control-label' %>
<div class="col-lg-8">
<%= f.text_field :website, class: 'form-control' %>
<%= f.hidden_field :id %>
<%= f.submit "Save changes", class: "btn btn-primary" %>
<% end %><!-- End of Form -->
最后,这是我正在使用的控制器。
class ProfilesController < ApplicationController
include ApplicationHelper
def index
# @profiles = Profile.limit(5)
@profiles = Profile.limit(15)
end
def show
@profile = Profile.find(params[:id])
end
def new
end
def create
@profile = Profile.new(profile_params)
if @profile.save
#successful save
flash[:success] = "Look at that. Ur Profile was ssaved"
redirect_to @profile
else
#unsuccesful
render 'new'
end
end
def edit
@user = current_user
@profile = Profile.find(@user.id)
end
def update
@profile = Profile.find(params[:id])
if @profile.update_attributes!(profile_params)
flash[:notice] = 'The User is successfully updated!'
redirect_to edit_profile_path
else
flash[:error] = @profile.errors.full_messages[0]
redirect_to edit_user_path
end
end
def view
@profile = Profile.find(params[:id])
end
private
def profile_params
params.require(:profile).permit(:id, :user_id, :content, :title, :department, :school, :education, :academic_focus, :website)
end
end
这很奇怪,因为我使用的代码与 editing/creating 用户使用的代码几乎完全相同,但它不起作用。我认为这与数据库中的关系有关。
如有任何见解,我们将不胜感激。
谢谢。
编辑 #1
这是我提交表单时发生的情况的照片
编辑 #2
如果该图像太小,这里是一个更好的
http://i.imgur.com/Wu3v7Gu.png
编辑 #3
我将 form_for 参数更改为
<%= form_for @profile, url: profile_update_path(@profile) do |f| %>
现在提交表单时出现此错误
undefined method `profile_update_path' for #<#<Class:0x007fe1ae0e5618>:0x007fe1aca90458>
删除 attr_accessor :id 语句,它可能会以某种方式干扰 ActiveRecord 默认访问器。
我刚刚开始我的第二个 Rails 项目,但在一些看起来很基本的事情上遇到了一些麻烦。不过我完全糊涂了。
我设置了两个模型,用户和配置文件。个人资料 belongs_to 用户和用户 has_one 个人资料。 我现在正在为配置文件设置编辑表单,无法通过此操作。编辑表单正确加载并将数据插入数据库但是当我提交表单时出现以下错误
No route matches {:action=>"show", :controller=>"profiles", :format=>nil, :id=>#<Profile id: 7, user_id: 7, content: "Fill this in", created_at: "2015-01-08 19:08:22", updated_at: "2015-01-08 19:08:22", title: "fooobar", department: "one bar", school: "dsadsa", education: "two bar", academic_focus: "my bar is broken", website: "who bar is bokrbne">} missing required keys: [:id]
我的模型是这样的
Class User < ActiveRecord::Base
has_one :profile, :class_name => 'Profile', :foreign_key => 'user_id'
attr_accessor :remember_token
end
class Profile < ActiveRecord::Base
belongs_to :user, :class_name => "User", :foreign_key => 'user_id'
attr_accessor :id
validates :content, presence: true, length: {maximum: 5000}
validates :user_id, presence: true
end
这是表格。我尝试了多种方法来添加 url: 参数到 form_for 但没有成功。
<%= form_for @profile do |f| %>
<%= f.label :title, class: 'col-lg-3 control-label' %>
<%= f.text_field :title, class: 'form-control' %>
<%= f.label :department, class: 'col-lg-3 control-label' %>
<%= f.text_field :department, class: 'form-control' %>
<%= f.label :school, class: 'col-lg-3 control-label' %>
<%= f.text_field :school, class: 'form-control' %>
<%= f.label :education, class: 'col-lg-3 control-label' %>
<%= f.text_field :education, class: 'form-control' %>
<%= f.label :academic_focus, class: 'col-lg-3 control-label' %>
<%= f.text_field :academic_focus, class: 'form-control' %>
<%= f.label :website, class: 'col-lg-3 control-label' %>
<div class="col-lg-8">
<%= f.text_field :website, class: 'form-control' %>
<%= f.hidden_field :id %>
<%= f.submit "Save changes", class: "btn btn-primary" %>
<% end %><!-- End of Form -->
最后,这是我正在使用的控制器。
class ProfilesController < ApplicationController
include ApplicationHelper
def index
# @profiles = Profile.limit(5)
@profiles = Profile.limit(15)
end
def show
@profile = Profile.find(params[:id])
end
def new
end
def create
@profile = Profile.new(profile_params)
if @profile.save
#successful save
flash[:success] = "Look at that. Ur Profile was ssaved"
redirect_to @profile
else
#unsuccesful
render 'new'
end
end
def edit
@user = current_user
@profile = Profile.find(@user.id)
end
def update
@profile = Profile.find(params[:id])
if @profile.update_attributes!(profile_params)
flash[:notice] = 'The User is successfully updated!'
redirect_to edit_profile_path
else
flash[:error] = @profile.errors.full_messages[0]
redirect_to edit_user_path
end
end
def view
@profile = Profile.find(params[:id])
end
private
def profile_params
params.require(:profile).permit(:id, :user_id, :content, :title, :department, :school, :education, :academic_focus, :website)
end
end
这很奇怪,因为我使用的代码与 editing/creating 用户使用的代码几乎完全相同,但它不起作用。我认为这与数据库中的关系有关。
如有任何见解,我们将不胜感激。
谢谢。
编辑 #1
这是我提交表单时发生的情况的照片
编辑 #2 如果该图像太小,这里是一个更好的 http://i.imgur.com/Wu3v7Gu.png
编辑 #3 我将 form_for 参数更改为
<%= form_for @profile, url: profile_update_path(@profile) do |f| %>
现在提交表单时出现此错误
undefined method `profile_update_path' for #<#<Class:0x007fe1ae0e5618>:0x007fe1aca90458>
删除 attr_accessor :id 语句,它可能会以某种方式干扰 ActiveRecord 默认访问器。