加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 使用has_one和belongs_to的嵌套路由和form_for

发布时间:2020-12-17 02:36:34 所属栏目:百科 来源:网络整理
导读:如何使用嵌套路由映射has_one模型以及如何在RESTful数据库之后为/localhost:3000/users/1/profile/new,html.erb添加form_for? 用户有一个个人资料. 楷模 class Profile ActiveRecord::Base attr_accessible :name,:surname belongs_to :userendclass User A
如何使用嵌套路由映射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

意见/型材/ 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>

controller: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

助手:SessionHelper(用于说明current_user)
????模块SessionsHelper
????????def sign_in(用户)
????????cookies.permanent [:remember_token] = user.remember_token
????????self.current_user = 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

解决方法

您的个人资料表是否具有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

你真的希望用户像这样创建他的个人资料吗?通常,您会在用户注册时创建配置文件.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读