将一个外表传递给" form_for"

时间:2015-06-11 13:27:53

标签: ruby-on-rails

我有两个型号

user.rb

class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable

  has_one :personal
  before_create :generate_profile
end

和personal.rb

class Personal < ActiveRecord::Base
   belongs_to :user
   validates :user_id, presence: true
end

在我的控制器中我有:

def createinfo
    @user = current_user
    @personal = @user.personal
end 

我想做一个这样的表格:

<%= form_for @personal do |f| %>
    Trigramme : <%= f.text_field :trigramme, :input_html => { :value => 'blabla' } %><br />
    Nom : <%= f.text_field :nom %><br />
    Prenom : <%= f.text_field :prenom %><br />
    Poste    : <%= f.text_field :poste %><br />
    Bio    : <%= f.text_area :bio %><br />
    <%= f.submit %>
<% end %>

但是我有一个错误,我不知道如何重新开始。我认为current_user.personal不接受路径form_for,但是当我只在代码中添加current_user.personal.trigramme时,它会打印正确的内容。

undefined method `personal_path' for #<#<Class:0x8e15a28>:0x8e15308>

你知道怎么做这个表格吗?

编辑:我的路线.rb

Rails.application.routes.draw do

  devise_for :users
  root 'static_pages#cvpage'
  get 'home'        => 'static_pages#home'
  get 'help'        => 'static_pages#help'
  get 'about'       => 'static_pages#about'
  get 'contact'     => 'static_pages#contact'
  get 'cvpage'      => 'static_pages#cvpage'
  get 'createinfo'  => 'static_pages#createinfo'
end

编辑2 schema.rb:

create_table "personals", force: :cascade do |t|
  t.string   "trigramme"
  t.string   "nom"
  t.string   "prenom"
  t.string   "poste"
  t.text     "bio"
  t.integer  "user_id"
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false
end

add_index "personals", ["user_id"], name: "index_personals_on_user_id"

create_table "users", force: :cascade do |t|
  t.string   "email",                  default: "", null: false
  t.string   "encrypted_password",     default: "", null: false
  t.string   "reset_password_token"
  t.datetime "reset_password_sent_at"
  t.datetime "remember_created_at"
  t.integer  "sign_in_count",          default: 0,  null: false
  t.datetime "current_sign_in_at"
  t.datetime "last_sign_in_at"
  t.string   "current_sign_in_ip"
  t.string   "last_sign_in_ip"
  t.datetime "created_at",                          null: false
  t.datetime "updated_at",                          null: false
end

1 个答案:

答案 0 :(得分:0)

YEAHH!我解决了我的问题(花了很多时间)

我把personal控制器放进去了:

def update
  @user = current_user
  @personal = @user.personal
  if @personal.update_attributes(personal_params)
    # if update is successful
  else
    # if update is unsuccessful
  end
  redirect_to home_path
end

def personal_params
  params.require(:personal).permit(:trigramme, :nom, :prenom, :poste, :bio)
end

我把这个表格放在我看来:

<%= form_for(@personal, html: { method: :put }) do |f| %>

Trigramme : <%= f.text_field :trigramme %><br />
Nom : <%= f.text_field :nom %><br />
Prenom : <%= f.text_field :prenom %><br />
Poste    : <%= f.text_field :poste %><br />
Bio    : <%= f.text_area :bio %><br />
<%= f.submit %>

谢谢大家!