위치, 전기, blah, blah, blah와 같은 항목으로 Devise Users를위한 별도의 프로필 모델을 만들려고합니다. 문제는 데이터베이스에 저장할 수 없다는 것입니다.
내 사용자를 "아티스트"라고합니다.
### /routes.rb ###
get 'artists/:id/new_profile' => 'artists/profiles#new', as: :profile
post 'artists/:id/new_profile' => 'artists/profiles#create'
### artists/profiles_controller.rb ###
class Artists::ProfilesController < ApplicationController
before_action :authenticate_artist!
def new
@artist = current_artist
@profile = ArtistProfile.new
end
def create
@artist = current_artist
@profile = ArtistProfile.new(profile_params)
if @profile.save
redirect_to current_artist
else
render 'new'
end
end
end
### /artist.rb ###
class Artist < ActiveRecord::Base
devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, :confirmable, :lockable, :timeoutable
has_one :artist_profile, dependent: :destroy
### /artist_profile.rb ###
class ArtistProfile < ActiveRecord::Base
belongs_to :artist
validates :artist_id, presence: true
end
### /views/artists/profiles/new.html.erb ###
<%= form_for(@profile, url: profile_path) do |f| %>
<div class="field">
<%= f.label :biography, "biography", class: "label" %>
<%= f.text_area :biography, autofocus: true , class: "text-field" %>
</div>
<div class="field">
<%= f.label :location, "location", class: "label" %>
<%= f.text_field :location, class: "text-field" %>
</div>
...
...
...
<div class="actions">
<%= f.submit "create profile", class: "submit-button" %>
</div>
<% end %>
내가 여기서 뭘 잘못하고 있니?
current_artist
개체를 사용하여 프로필을 초기화해야 합니다.
class Artists::ProfilesController < ApplicationController
before_action :authenticate_artist!
def new
@artist = current_artist
@profile = @artist.build_profile
end
def create
@artist = current_artist
@profile = @artist.build_profile(profile_params)
if @profile.save
redirect_to current_artist
else
render 'new'
end
end
end
최신 정보:
이 예를 사용하려면 협회가
class Artist < ActiveRecord::Base
has_one :profile, class_name: ArtistProfile
end
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다