저는 레일스에서 처음으로 소유권을 얻으려고 애쓴다. 나는 그림이 예술가에게 속하고 예술가는 많은 그림을 가질 수있는 앱을 가지고있다. 내 그림을 만들고 편집 할 수 있지만 콘솔을 통하지 않고는 아티스트를 편집하거나 만들 수 없습니다. 많은 인터넷 검색을 통해 나는 내 자신이 돌아 서 있다고 느낍니다. 어떤 도움을 많이 주시면 감사하겠습니다!Rails 4에서 belongs_to 레코드를 만들거나 업데이트 할 수 없습니다.
가 여기 내 routes.rb 파일입니다 : 여기
MuseumApp::Application.routes.draw do
resources :paintings
resources :paintings do
resources :artists
resources :museums
end
root 'paintings#index'
end
여기 내 그림의 컨트롤러
def show
@painting = Painting.find params[:id]
end
def new
@painting = Painting.new
#@artist = Artist.new
end
def create
safe_painting_params = params.require(:painting).permit(:title, :image)
@painting = Painting.new safe_painting_params
if @painting.save
redirect_to @painting
else
render :new
end
end
def destroy
@painting = Painting.find(params[:id])
@painting.destroy
redirect_to action: :index
end
def edit
@painting = Painting.find(params[:id])
end
def update
@painting = Painting.find(params[:id])
if @painting.update_attributes(params[:painting].permit(:title, :image)) #safe_params
redirect_to @painting
else
render :edit
end
end
의 내 그림의 형태로보기 :
<%= form_for(@painting) do |f| %>
<fieldset>
<legend>painting</legend>
<div>
<%= f.label :title %>
<%= f.text_field :title %>
</div>
<div>
<%= f.label :image %>
<%= f.text_field :image %>
</div>
<%= form_for([@painting,@painting.create_artist]) do |f| %>
<div>
<%= f.label :Artist %>
<%= f.text_field :name %>
</div>
</fieldset>
<%= f.submit %>
<% end %>
<% end %>
연예인 컨트롤러 :
class ArtistsController < ApplicationController
def index
@artists = Artist.all
@artists = params[:q] ? Artist.search_for(params[:q]) : Artist.all
end
def show
@artist = Artist.find params[:id]
end
def new
@artist = Artist.new
end
def create
@painting = Painting.find(params[:painting_id])
@artist = @painting.create_artist(artist_params)
redirect_to painting_path(@painting)
end
def destroy
@artist = Artist.find(params[:id])
@Artist.destroy
redirect_to action: :index
end
def edit
@artist = Artist.find(params[:id])
end
def update
@painting = Painting.find(params[:painting_id])
@artist = @artist.update_attributes(artist_params)
redirect_to painting_path(@painting)
end
end
private
def artist_params
params.require(:artist).permit(:name)
end
인덱스보기 :
이<h1> Hello and Welcome to Museum App</h1>
<h3><%= link_to "+ Add To Your Collection", new_painting_artist_path %></h3>
<%= form_tag '/', method: :get do %>
<%= search_field_tag :q, params[:q] %>
<%= submit_tag "Search" %>
<% end %>
<br>
<div id="paintings">
<ul>
<% @paintings.each do |painting| %>
<li><%= link_to painting.title, {action: :show, id:painting.id} %> by <%= painting.artist_name %></li>
<div id = "img">
<br><%= link_to (image_tag painting.image), painting.image %><br>
</div>
<%= link_to "Edit", edit_painting_path(id: painting.id) %>
||
<%= link_to 'Destroy', {action: :destroy, id: painting.id},method: :delete, data: {confirm: 'Are you sure?'} %>
<% end %>
</ul>
</div>
중첩 된 속성에 대해 읽어 볼 필요가 있습니다. 네스트 1 형식을 또 다른 –
에 중첩시킬 수는 없으며 Google에 계속 연락하고 레일스 가이드를 읽으십시오. 양식이 나에게 보이지 않았지만, "디 네스트 (de-nested)"할 때 아티스트의 제목 방법이 없다는 오류가 발생했습니다. – edswartz