问题描述
如何通过RESTful数据库映射具有嵌套路由的has_one模型以及如何为/localhost:3000/users/1/profile/new,html.erb添加form_for?
How to map out has_one model with nested routes and how to add a form_for for /localhost:3000/users/1/profile/new,html.erb following RESTful database?
用户有一个配置文件。
模型
class Profile < ActiveRecord::Base
attr_accessible :name, :surname
belongs_to :user
end
class User < ActiveRecord::Base
attr_accessible :email, :email_confirmation, :password, :password_confirmation
has_secure_password
has_one :profile, dependent: :destroy
end
resources :users do
resources :profiles (note: has_one profile)
resources :progress_charts
resources :calories_journals
end
views / profiles / new.html.erb
views/profiles/new.html.erb
<h1>About You</h1>
<div class="row">
<div class="span6 offset3">
<%= form_for(@profile) do |f| %>
<%= render 'shared/error_messages' %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :surname %>
<%= f.text_field :surname %>
<%= f.submit "Create my account", class: "btn btn-large btn-primary" %>
<% end %>
</div>
</div>
控制器:Profiles_controller.rb
我发现了两个错误,因为我不太明白为什么
controller: Profiles_controller.rbTwo errors I have discovered as I do not quite understand why it wasn't working.
class ProfilesController < ApplicationController
def index
end
def show
end
def new
# @user = User.find(params[:id]) # Error message: Couldn't find User without an ID
# @profile = @user.build_profile()
@profile = current_user.build_profile(params[:id]) # Error message: unknown attributes: user_id
end
def edit
end
def create
end
def update
end
def destroy
end
end
Helpers:SessionHelper(以说明current_user)
模块SessionsHelper
def sign_in(用户)
cookies.permanent [:remember_token] = user.remember_token
self.current_user =用户
结尾
Helpers: SessionHelper (to illustrate current_user) module SessionsHelper def sign_in(user) cookies.permanent[:remember_token] = user.remember_token self.current_user = user end
def signed_in?
!current_user.nil?
end
def current_user=(user)
@current_user = user
end
def current_user?(user)
user == current_user
end
def current_user
@current_user ||= User.find_by_remember_token(cookies[:remember_token])
end
def sign_out
self.current_user = nil
cookies.delete(:remember_token)
end
def signed_in_user
unless signed_in?
store_location
redirect_to signin_path, notice: "Please sign in."
end
end
end
推荐答案
您的个人资料表是否具有user_id属性?
Does your profiles table has a user_id attribute?
在您的路线中,个人资料应为单数,因为用户有一个个人资料:
In your routes, profile should be singular, since a user has one profile:
resources :users do
resource :profile
resources :progress_charts
resources :calories_journals
end
到用户配置文件的路由为 users /:user_id / profile
(而不是 users /:user_id / profile /:id
The route to a user profile will be users/:user_id/profile
(and not users/:user_id/profile/:id
在您的profiles_controller中:
In your profiles_controller:
@profile = current_user.build_profile(params[:id]) # why params[:id]?
#it should just be
@profile = current_user.build_profile()
@user = User.find(params[:user_id])
格式如下:
form_for [@user, @profile] do |f|...
end
您真的要用户像那样创建他的个人资料?通常,您将在用户注册时创建配置文件。
Do you really want the user to create his profile like that? Usually, you would create the profile when a user register.
这篇关于嵌套的路线,form_for和使用has_one和belongs_to的模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!