【发布时间】:2015-11-23 14:10:54
【问题描述】:
我创建了模型用户和模型配置文件。在我的主页上,我在下拉菜单导航栏中有一个链接,该链接指向编辑个人资料。 我面临的问题是“没有路由匹配 {:action=>"edit", :controller=>"profiles", :id=>nil} 缺少必需的键:[:id]”。
编辑页面的路径是“edit_profile_path”,动词 GET 和 URI 模式“/profiles/:id/edit(.:format)”。我很难插入“id”。下面是我在我的应用程序上的代码。
在模型配置文件中我有:
class Profile < ActiveRecord::Base
belongs_to :user, dependent: :destroy
end
在模型用户文件中我有:
class User < ActiveRecord::Base
has_one :profile
end
配置文件有许多属性,但其中之一是“user_id”,它是一个等于用户 ID 的整数。所以 ID#5 的用户#5 是 Profile#5 的所有者。 这是我在视图文件中的代码:
<li><%= link_to "Edit Profile", edit_profile_path(@profile) %></li>
关于上面的代码,我尝试在括号内插入不同的代码,来自@profile.id、@profile、@user.id 和@user。但它没有奏效。
我创建了一个配置文件控制器,我认为(但我不确定)我的问题来自profiles_controller 文件。这是我拥有的代码:
class ProfilesController < ApplicationController
before_action :authenticate_user!
before_action :set_profile, only: [:edit, :update]
def edit
end
def new
@profile = Profile.new
end
def create
@profile = Profile.new(profile_params)
@profile.user_id = current_user.id
if @profile.save
redirect_to welcome_path
else
render 'new'
end
end
def update
@profile.update(profile_params)
redirect_to welcome_path
end
private
def set_profile
@profile = Profile.find(params[:id])
end
end
【问题讨论】:
标签: ruby-on-rails hyperlink routes url-routing link-to