【问题标题】:nested routes and form_for and models using has_one and belongs_to嵌套路由和 form_for 以及使用 has_one 和 belongs_to 的模型
【发布时间】:2012-07-05 08:40:20
【问题描述】:

如何使用嵌套路由映射 has_one 模型以及如何在 RESTful 数据库之后为 /localhost:3000/users/1/profile/new,html.erb 添加 form_for?

用户有一个个人资料。

型号

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

<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 我发现了两个错误,因为我不太明白为什么它不起作用。

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 登录(用户) cookies.permanent[:remember_token] = user.remember_token self.current_user = 用户 结束

  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

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 controllers form-for nested-resources


    【解决方案1】:

    您的配置文件表是否有 user_id 属性?

    在您的路线中,个人资料应该是单数,因为用户只有一个个人资料:

    resources :users do
        resource  :profile
        resources :progress_charts
        resources :calories_journals
    end
    

    用户个人资料的路径将是users/:user_id/profile(而不是users/:user_id/profile/:id

    在您的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
    

    您真的希望用户这样创建个人资料吗?通常,您会在用户注册时创建个人资料。

    【讨论】:

    • 感谢您的回复,最初我认为配置文件属性应该是用户,但我没有找到关于一个模型的信息,许多表单不像许多关于单个表单中嵌套模型的文章。所以我决定把它分解成更小的组件,比如 email * pwd form -> About user -> User Goal。在 profile_controller 中,我收到了应该可以工作的错误消息,但我有点强调为什么它不起作用。查看带有哈希的两条错误消息。
    • 简单的错误!我编辑了 schema.rb 而不是迁移 rb 导致错误!
    • 永远不要编辑 schema.rb,每次迁移时都会重新创建它。如果您需要编辑架构,请运行 rails g migration name_of_the_migration,然后在此处写入您的更改。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多