Railsアプリでメソッドエラーがないのはなぜですか?
-
28-10-2019 - |
質問
私はこれが初心者であり、私は価値がないことを知っていますが、誰かが私にメソッドなしエラーを受け取っている理由を説明してもらえますか?これが私がしたことです。データベースの新しい移行を既存のRailsアプリに生成し、移行は「プロファイル」と呼ばれます。私はDBを実行しました:移行してから、以前の「new.html.erb」フォームの編集に進みました。コードは次のようになります:
class CreateProfiles < ActiveRecord::Migration
def self.up
create_table :profiles do |t|
t.string :major
t.string :year
t.string :books_sell
t.string :books_buy
t.string :facebook
t.string :restaurants
t.string :interests
t.timestamps
end
add_index :profiles, :major
add_index :profiles, :year
add_index :profiles, :books_sell
add_index :profiles, :books_buy
add_index :profiles, :facebook
add_index :profiles, :restaurants
add_index :profiles, :interests
end
def self.down
drop_table :profiles
end
end
基本的に、アプリにプロファイルセクションを追加していますが、これを取得しています。
undefined method `major' for #<User:0x00000100b6e030>
Extracted source (around line #23):
20: </div>
21: <div class="field">
22: <%= f.label :"major" %><br />
23: <%= f.text_field :major %>
24: </div>
これは私のviews/users/new.hmtl.erbファイルです。
<h1>Sign up</h1>
<%= form_for(@user) do |f| %>
<%= render 'shared/error_messages', :object => f.object %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :email %><br />
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :password %><br />
<%= f.password_field :password %>
</div>
<div class="field">
<%= f.label :password_confirmation, "Confirmation" %><br />
<%= f.password_field :password_confirmation %>
</div>
<div class="field">
<%= f.label :"year" %><br />
<%= f.text_field :year %>
</div>
<div class="field">
<%= f.label :"major" %><br />
<%= f.text_field :major %>
</div>
<div class="actions">
<%= f.submit "Sign up" %>
</div>
<% end %>
何が欠けていますか?
解決
ここでの問題は、以前にユーザーモデルの下でフォームビューが完成したことです。私はそのフォームにタックしたかったので、プロファイルという名前の新しい移行を作成しました。ユーザーモデルの移行を手動でロールバックできず、文字列や列をタックするだけでは、これをしました。
ただし、ユーザーモデルの下にプロファイルモデルからテキストフィールドを追加すると、エラーが発生します。
代わりに私がしたことは、私が作成したことでした Add_xxx_to_yyy
移行により、問題なく以前に作成された移行に柱を追加できました。使った rails generate migration Add_profile_to_User
私はRails 3.0を使用しているので、アンダースコアがあります(私がやったときはうまくいきませんでした Addprofiletouser
)。 etvoilà!
他のヒント
あなたはあなたの移行を貼り付けました Profile
モデル。そして、私はあなたの中でそれを推測します @user
変数新しいインスタンスがあります User
モデル。
そして、方法や属性がないので major
ユーザーに定義されていると、「未定義の方法...」という苦情が表示されます。