无法在Rails 4中创建或更新belongs_to记录

时间:2014-04-19 15:57:19

标签: ruby-on-rails

我是Rails的新手,并努力让我的belongs_to协会正确。我有一个应用程序,其中一幅画属于一个艺术家,一个艺术家可以有很多画。我可以创建和编辑我的画作,但除了通过控制台,我无法编辑或创建艺术家。通过谷歌搜索,我觉得我已经转过身来。任何帮助将不胜感激!

这是我的routes.rb文件:

MuseumApp::Application.routes.draw do
  resources :paintings

  resources :paintings do
  resources :artists
  resources :museums
end
  root 'paintings#index'

end

这是我的画作Controller

   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 个答案:

答案 0 :(得分: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 %>

注意:

您应该让Artist Controller至少使用newcreateeditupdate方法来实现此目标。

修改

尝试反向

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 %>

将此表格放在绘画视图中。