2014-09-23 20 views
0

我有一個嵌套資源的表單有問題。數據模型很簡單:嵌套資源的表單:不能保持關聯

class Event < ActiveRecord::Base 
    extend FriendlyId 
    friendly_id :name, use: [:slugged, :finders] 
    has_many :event_contents 
end 

class EventContent < ActiveRecord::Base 
    belongs_to :event 
end 

我的形式:

= simple_form_for([:admin, @event, @event.event_contents.new], remote: true) do |f| 
    .chat-form 
    .input-cont 
     = f.input :content, label: false, input_html: { class: 'form-control' }  
    .btn-cont 
     %span.arrow 
     = f.submit 'invia', class: 'btn blue icn-only' 

控制器:

class Admin::EventContentsController < AdminController 
    def create 
    @event_content = EventContent.new event_content_params 
    @event_content.user_id = current_user.id if current_user 
    if @event_content.save 
     respond_to do |format| 
     format.js { render :nothing => true } 
     end 
    else 

    end 
    end 

    private 
    def event_content_params 
    params.require(:event_content).permit(
     :content, 
     :event_id, 
     :user_id 
    ) 
    end 
end 

當我在PARAMS代替事項標識提交後我有事件「蛞蝓「

pry(#<Admin::EventContentsController>)> params 
=> {"utf8"=>"✓", "event_content"=>{"content"=>"uhm"}, "commit"=>"invia", "action"=>"create", "controller"=>"admin/event_contents", "event_id"=>"test-test-test"} 

該記錄是在db中創建,但event_id是零,所以關聯被破壞。 爲什麼不是event_id我有事件slug ???

更新

的問題是控制器:

def create 
    @event = Event.find params[:event_id] 
    @event_content = @event.event_contents.build event_content_params 
    @event_content.user_id = current_user.id if current_user 
    if @event_content.save 
     respond_to do |format| 
     format.js 
     end 
    else 

    end 
    end 

回答

0

也許嘗試執行以下操作:

    在事件模型
  1. 在表單中添加accepts_nested_attributes_for :event_contents
  2. ,當你正在收集:event_contents替換

    f.input :content, label: false, input_html: { class: 'form-control' } 
    

    下列要求:

    f.fields_for :event_contents do |content| 
    content.input :content 
    end 
    
  3. 更新您的大力PARAMS在活動控制器包括{:event_contents_attributes}這可能看起來像下面這取決於你需要通過其他PARAMS通過:

    ​​
  4. 在您的活動控制器中,更新def new包括此行項目event_content = @event.event_contents.build

所有這一切說,我相信你會希望這個被路由到您的活動控制器,而不是你的EventContents控制器,因爲:event_contents嵌套在:事件。看起來您的表單目前正在提交給EventContents控制器。另外,我不相信你的simple_form_for @event.event_contents.new這個參數是必要的。

這是我建立你的問題。這不是管理員名稱空間,但可能會有所幫助。

class EventsController < ApplicationController 


def new 
    @event = Event.new 
    event_content = @event.event_contents.build 
    end 

def create 
    @event = Event.new(event_params) 
    respond_to do |format| 
     if @event.save 
      format.html { redirect_to @event, notice: 'Event was successfully created.' } 
      format.json { render :show, status: :created, location: @event } 
      format.js 
     else 
      format.html { render :new } 
      format.json { render json: @event.errors, status: :unprocessable_entity } 
     end 
    end 
private 
def event_params 
     params.require(:event).permit(:name, {:event_contents_attributes => [:content]}) 
    end 
end 

事件模型:

class Event < ActiveRecord::Base 
    has_many :event_contents 
    accepts_nested_attributes_for :event_contents 
end 

最後形式:

<%= form_for(@event) do |event| %> 

    <div class="field"> 
    <%= event.label :name %><br> 
    <%= event.text_field :name %> 
    </div> 

    <%= event.fields_for :event_contents do |content| %> 
     <div class="field"> 
     <%= content.label :content %> 
     <%= content.text_field :content %> 
    </div> 
    <% end %> 

    <div class="actions"> 
    <%= event.submit %> 
    </div> 
<% end %> 
+0

發現問題... – 2014-09-24 07:44:01