2014-04-19 1 views
0

저는 레일스에서 ​​처음으로 소유권을 얻으려고 애쓴다. 나는 그림이 예술가에게 속하고 예술가는 많은 그림을 가질 수있는 앱을 가지고있다. 내 그림을 만들고 편집 할 수 있지만 콘솔을 통하지 않고는 아티스트를 편집하거나 만들 수 없습니다. 많은 인터넷 검색을 통해 나는 내 자신이 돌아 서 있다고 느낍니다. 어떤 도움을 많이 주시면 감사하겠습니다!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

중첩 된 속성에 대해 읽어 볼 필요가 있습니다. 네스트 1 형식을 또 다른 –

+0

에 중첩시킬 수는 없으며 Google에 계속 연락하고 레일스 가이드를 읽으십시오. 양식이 나에게 보이지 않았지만, "디 네스트 (de-nested)"할 때 아티스트의 제목 방법이 없다는 오류가 발생했습니다. – edswartz

답변

0

귀하의 경우에는 당신이 이것을 달성하기 위해 accepts_nested_attributes_forfields_for를 사용해야합니다.

Artist 

has_many :paintings, :dependent => :destroy   
accepts_nested_attributes_for :paintings 

Painting 

belongs_to :artist 

또한이

form_for(@artist) do |f| %> 

<fieldset> 
    <legend>Artist</legend> 

     <%= f.label :Artist %> 
     <%= f.text_field :name %> 

     <%= fields_for :paintings, @artist.paintings do |artist_paintings| %> 
     <%= artist_paintings.label :title %> 
     <%= artist_paintings.text_field :title %> 

     <%= artist_paintings.label :image %> 
     <%= artsist_paintings.text_field :image %> 

    </fieldset> 
    <%= f.submit %> 
<% end %> 

주처럼 그림아티스트를 작성하려고합니다

당신은 적어도 new, createArtist Controller을 가지고해야한다, editupdate 방법 이것을 달성하기 위해 정의되었습니다.

편집 반대

Artist 

    has_many :paintings, :dependent => :destroy   


Painting 

    belongs_to :artist 
    accepts_nested_attributes_for :paintings 


    form_for(@painting) do |f| %> 

     <fieldset> 
      <legend>Painting</legend> 

      <%= f.label :title %> 
      <%= f.text_field :title %> 

      <%= f.label :image %> 
      <%= f.text_field :image %> 

      <%= fields_for :artists, @painting.artists do |ff| %> 
      <%= ff.label :Artist %> 
      <%= ff.text_field :name %> 


      </fieldset> 
      <%= f.submit %> 
     <% end %> 

넣어에게 그림의 전망이 양식을보십시오.

+0

감사합니다. @Pavan. 위의 form_for (@artist) ... 코드가 페인팅 컨트롤러 또는 아티스트 컨트롤러에 있어야한다고 말하고 있습니까? 내가 가지고있는 폼을 바꿀 때 정의되지 않은 메서드 'artists_path'가 생긴다. – edswartz

+0

@ user10596 내가 말했듯이 이것을 위해 아티스트 컨트롤러가 있어야한다. – Pavan

+0

죄송합니다. 그림보기 또는 아티스트보기에서 양식 코드를 가져와야하는지 묻고 싶습니다. 내 아티스트 컨트롤러를 게시 할 것입니다 – edswartz

관련 문제