2016-05-27 5 views
0

データベースにコードを記述するアプリケーションに問題があります。私はパラメータで見ることができますが、フォームを送信した後はデータベースには表示されません。データベースに書き込まれていない多相ネストされた属性

私はコースオブジェクトに追加したいアドレスオブジェクトを作成しました。

モデル

class Course < ActiveRecord::Base 
    has_one :address, :as => :addressable 
    accepts_nested_attributes_for :address 
    // more code 
end 

class Address < ActiveRecord::Base 
    belongs_to :addressable, :polymorphic => true 
end 

コントローラ

class CoursesController < ApplicationController 
    def course_params 
    params.require(:course).permit(:short_description, :picture, :name, :cost, :description, :occurs_at, address_attributes: [:line1, :line2, :city, :state, :zip]) 
end 

def create 
    @course = current_user.courses.build(course_params) 
    @course.build_address 
    if @course.save 
    flash[:success] = "Course created!" 
    redirect_to root_url 
    else 
    @feed_items = [] 
    render 'static_pages/home' 
    end 
end 

//No address controller 

私はそれをデータベースに書き込むように見えるが、実際には2 ...

を関連付けない、保存の内側にbuild_addressコールを移動しようとしました

ヘルパー

module FormHelper 
    def setup_course(course) 
    course.address ||= Address.new 
    course 
    end 
end 
私はレール4

を実行しています

ビュー

<%= form_for(setup_course(@course), html: { multipart: true }) do |f| %> 
    <%= f.fields_for :address do |address| %> 
    <%= render :partial => 'shared/address', :locals => {:f => address} %> 
    <% end %> 
<% end %> 

データベースコンソール

Processing by CoursesController#create as HTML 
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"KKDEbmm7vBFjDR6KPHn29ZbroA6FyLtkUaJhBu5kAELeL1Zs89WmonpDH8wCCqtFeiMDXxypJwsHv/ck5wbznA==", "course"=>{"name"=>"11", "short_description"=>"fdsaf0dsa3f032ds", "description"=>"f03dsaf1dsa01", "occurs_at"=>"<date>", "cost"=>"", "address_attributes"=>{"line1"=>"aa", "line2"=>"bb", "city"=>"cc", "state"=>"wa", "zip"=>"12345"}}, "commit"=>"Create Course"} 
    User Load (1.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]] 
(0.1ms) begin transaction 
SQL (0.5ms) INSERT INTO "courses" ("short_description", "name", "description", "user_id", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?) [["short_description", "fdsaf0dsa3f032ds"], ["name", "11"], ["description", "f03dsaf1dsa01"], ["user_id", 1], ["created_at", "2016-05-27 02:33:31.234818"], ["updated_at", "2016-05-27 02:33:31.234818"]] 
SQL (0.4ms) INSERT INTO "addresses" ("addressable_type", "addressable_id", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["addressable_type", "Course"], ["addressable_id", 17], ["created_at", "2016-05-27 02:33:31.247646"], ["updated_at", "2016-05-27 02:33:31.247646"]] 
(13.6ms) commit transaction 

はあなたの助けをありがとう!

+0

あなたCoursesControllerは次のようになり、アクションメソッドは何を作るのか? – Anand

+0

本当ですか?ログには、コースと住所の両方に挿入されているレコードが表示されます。 – max

+0

私はcourses_controllerにcreateを追加しました。 @Anand – bralyan

答えて

0

問題はAnandが尋ねた問題です。それは私の作成機能の問題です。

簡単な修正:

def create 
    @course = current_user.courses.build(course_params) 
    if @course.save 
    flash[:success] = "Course created!" 
    redirect_to root_url 
    else 
    @feed_items = [] 
    render 'static_pages/home' 
    end 
end 
関連する問題