2014-04-19 73 views
0

我是Rails的新手,努力获得我的belongs_to协会的权利。我有一个应用程序,一幅绘画属于一位艺术家,一位艺术家可以拥有绘画。我可以创建和编辑我的绘画,但是除了通过控制台之外,我无法编辑或创建艺术家。通过很多谷歌搜索,我觉得我已经转身了。任何帮助将非常感激!无法在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

感谢并将继续谷歌和阅读Rails指南。这张表格看起来并不正确,但是当我“解除嵌套”时,我发现错误的是艺术家没有标题方法。 – 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 %> 

注:

您应该至少newcreate可以让您的Artist Controllereditupdate方法在其中定义来实现这一点。

编辑

尝试反向

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