我的问题涉及用户的index.html.erb
,其代码发布在此帖的底部。从模型中可以看出,个人资料belongs_to
用户。
我正在尝试在用户index.html.erb
中找到用户个人资料的功能链接。
显然它没有工作因此这个问题。我得到的错误是:用户#index
中的`ActionController :: UrlGenerationError我知道user_profile_path
本身就是一条功能路径。但是,由于我将其插入用户的index.html.erb
页面而不是个人资料的index.html.erb
页面,因此它比平时更加诡异?
如何使用户的个人资料链接有效?
感谢您的帮助。 `
用户
class User < ApplicationRecord
has_many :profiles
end
配置文件
class Profile < ApplicationRecord
belongs_to :user
end
个人资料控制器
class ProfilesController < ApplicationController
before_action :set_profile, only: [:show, :edit, :update, :destroy]
# GET /profiles
# GET /profiles.json
def index
@user = User.find(params[:user_id])
@profiles = @user.profiles
end
# GET /profiles/1
# GET /profiles/1.json
def show
@user = User.find(params[:user_id])
@profile = @user.profiles.find(params[:id])
respond_to do |format|
format.html # show.html.erb
end
end
# GET /profiles/new
def new
@user = User.find(params[:user_id])
@profile = @user.profiles.build
end
# GET /profiles/1/edit
def edit
@user = User.find(params[:user_id])
@profile = @user.profiles.find(params[:id])
end
# POST /profiles
# POST /profiles.json
def create
@user = User.find(params[:user_id])
@profile = @user.profiles.new(profile_params)
respond_to do |format|
if @profile.save
format.html { redirect_to @profile.user, notice: 'Profile was successfully created.' }
format.json { render :show, status: :created, location: @profile }
else
format.html { render :new }
format.json { render json: @profile.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /profiles/1
# PATCH/PUT /profiles/1.json
def update
@user = User.find(params[:user_id])
@profile = @user.profiles.find(params[:id])
respond_to do |format|
if @profile.update(profile_params)
format.html { redirect_to @profile, notice: 'Profile was successfully updated.' }
format.json { render :show, status: :ok, location: @profile }
else
format.html { render :edit }
format.json { render json: @profile.errors, status: :unprocessable_entity }
end
end
end
# DELETE /profiles/1
# DELETE /profiles/1.json
def destroy
@user = User.find(params[:user_id])
@post = @user.profiles.find(params[:id])
@profile.destroy
respond_to do |format|
format.html { redirect_to profiles_url, notice: 'Profile was successfully destroyed.' }
format.json { head :no_content }
end
end
private
def set_user
@user = User.find(params[:user_id])
end
# Use callbacks to share common setup or constraints between actions.
def set_profile
@profile = @user.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def profile_params
params.require(:profile).permit(:user_name, :user_id)
end
end
用户个人资料
class UsersController < ApplicationController
before_action :set_user, only: [:show, :edit, :update, :destroy]
# GET /users
# GET /users.json
def index
@users = User.all
end
# GET /users/1
# GET /users/1.json
def show
end
# GET /users/new
def new
@user = User.new
end
# GET /users/1/edit
def edit
end
# POST /users
# POST /users.json
def create
@user = User.new(user_params)
respond_to do |format|
if @user.save
format.html { redirect_to @user, notice: 'User was successfully created.' }
format.json { render :show, status: :created, location: @user }
else
format.html { render :new }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /users/1
# PATCH/PUT /users/1.json
def update
respond_to do |format|
if @user.update(user_params)
format.html { redirect_to @user, notice: 'User was successfully updated.' }
format.json { render :show, status: :ok, location: @user }
else
format.html { render :edit }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
# DELETE /users/1
# DELETE /users/1.json
def destroy
@user.destroy
respond_to do |format|
format.html { redirect_to users_url, notice: 'User was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_user
@user = User.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def user_params
params.require(:user).permit(:first_name, :last_name, :email)
end
end
路线
Rails.application.routes.draw do
root to: 'users#index'
resources :users do
resources :profiles
end
end
用户的index.html.erb
<h1>Users</h1>
<table>
<thead>
<tr>
<th>First name</th>
<th>Last name</th>
<th>Email</th>
<th>User Profile</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @users.each do |user| %>
<tr>
<td><%= user.first_name %></td>
<td><%= user.last_name %></td>
<td><%= user.email %></td>
<td><%= link_to 'User Profile', user_profile_path(user) %>
<td><%= link_to 'Show', user %></td>
<td><%= link_to 'Edit', edit_user_path(user) %></td>
<td><%= link_to 'Destroy', user, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New User', new_user_path %>
修改
Rake Routes
Prefix Verb URI Pattern Controller#Action
root GET / users#index
user_profiles GET /users/:user_id/profiles(.:format) profiles#index
POST /users/:user_id/profiles(.:format) profiles#create
new_user_profile GET /users/:user_id/profiles/new(.:format) profiles#new
edit_user_profile GET /users/:user_id/profiles/:id/edit(.:format) profiles#edit
user_profile GET /users/:user_id/profiles/:id(.:format) profiles#show
PATCH /users/:user_id/profiles/:id(.:format) profiles#update
PUT /users/:user_id/profiles/:id(.:format) profiles#update
DELETE /users/:user_id/profiles/:id(.:format) profiles#destroy
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
编辑两位参考
下面的几个答案答案 0 :(得分:0)
从路线的外观
user_profile GET /users/:user_id/profiles/:id(.:format) profiles#show
您需要将用户ID和配置文件ID传递给路径。 如下所示:
<td><%= link_to 'User Profile', user_profile_path(user.id, user.profiles.first.id) %>
由于用户模型上的关系是has_many
,您需要指定要链接到的配置文件(我以第一个为例)。或者您可以使用user_profiles_path
答案 1 :(得分:0)
使用助手的地方并不重要。但是,由于用户has_many
个人资料,您需要user_profiles_path(user)
。
答案 2 :(得分:0)
user_profile GET /users/:user_id/profiles/:id(.:format) profiles#show
或者你这样做。因为您需要在动作节目
上传递个人资料ID<td><%= link_to 'User Profile', user_profile_path(user, user.profiles.first) %>
或者您必须更改为索引页面。
<td><%= link_to 'User Profile', user_profiles_path(user) %>
因为你与个人资料的关系是to_many,你必须确定动作显示你正在编辑的个人资料。
也许您与个人资料的关系应该是has_one。像一个用户有一个配置文件
答案 3 :(得分:0)
由于用户拥有多个配置文件,因此您无法指定单个用户配置文件的路径,而无需指定哪个配置文件。单个用户配置文件的路径帮助程序需要一个用户实例和一个特定的用户配置文件实例。在不知道您是想要显示用户的所有配置文件还是仅显示一个配置文件的情况下,我建议运行rake routes
并查看结果以找到您需要的帮助程序名称。
根据您的路线,如果您想使用user_profile_path
,我认为这是正确的电话:
user_profile_path(user, profile) # profile must be a valid instance of Profile, eg. `user.profiles.first`
如果您想要链接到用户的所有配置文件,这应该是正确的呼叫:
user_profiles_path(user)
但是,那么你想在ProfilesController#index中检查:user_id参数,并只显示该用户的配置文件。
示例:
if params[:user_id].present?
user = User.find(params[:user_id))
@profiles = user.profiles
else
@profiles = Profile.all
end