2017-01-26 11 views
0

イム関連付けるモデルとイムは、私は私のレストランのコントローラ関係モデルは

before_action :set_restaurant, only: [:show, :edit, :update, :destroy] 

def new 
    @restaurant = Restaurant.new 
    @restaurant.build_chef 
end 

def create 
    @restaurant = Restaurant.new(restaurant_params) 

    respond_to do |format| 
    if @restaurant.save 
     format.html { redirect_to @restaurant, notice: 'Restaurant was successfully created.' } 
     format.json { render action: 'show', status: :created, location: @restaurant } 
    else 
     format.html { render action: 'new' } 
     format.json { render json: @restaurant.errors, status: :unprocessable_entity } 
    end 
    end 
end 

def set_restaurant 
    @restaurant = Restaurant.find(params[:id]) 
end 

def restaurant_params 
    params.require(:restaurant).permit(:avatar, :name, :description, 
    chef_attributes: [ :avatar, :name ] 
    ) 
end 

を持っており、これはcreate.html.erb

<%= form_for(@restaurant, multipart: true) do |f| %> 
    <div class="field"> 
    <%= f.label :restaurant_name %> 
    <%= f.text_field :name %> 
    </div> 
    <div class="field"> 
    <%= f.label :description %> 
    <%= f.text_field :description %> 
    </div> 
    <div class="field"> 
    <%= f.label :image %> 
    <%= f.file_field :avatar %> 
    </div> 

    <%= f.fields_for :chef do |f| %> 
    <div class="field"> 
     <%= f.label :chef_name %> 
     <%= f.text_field :name %> 
    </div> 
    <div class="field"> 
     <%= f.label :image %> 
     <%= f.file_field :avatar %> 
    </div> 
    <% end %> 
    <div class="actions"> 
    <%= f.submit 'Add new restaurant' %> 
    <%= link_to 'Nevermind', restaurants_path, class: 'button' %> 
    </div> 
<% end %> 

でのレンダリングイム私_formである、これは私です立ち往生シェフモデル

class Chef < ApplicationRecord 
    belongs_to :restaurant 

    validates :name, presence: true 
    validates :avatar, 
    attachment_content_type: { content_type: /\Aimage\/.*\Z/ }, 
    attachment_size: { less_than: 5.megabytes } 

    has_attached_file :avatar, styles: { 
    thumb: '100x100>', 
    square: '200x200#', 
    medium: '300x300>' 
    } 
end 

、これが私のレストランのモデル

0であります
class Restaurant < ApplicationRecord 
    has_one :chef 
    accepts_nested_attributes_for :chef 

    validates :name, presence: true 
    validates :avatar, 
    attachment_content_type: { content_type: /\Aimage\/.*\Z/ }, 
    attachment_size: { less_than: 5.megabytes } 

    has_attached_file :avatar, styles: { 
    thumb: '100x100>', 
    square: '200x200#', 
    medium: '300x300>' 
    } 
end 

私の問題は、私が新しいレストランをセーブすると、何も起こらずにフォームにとどまることです。私は何が起こっているのか分かりません。サポート。

Log

+0

これで問題は解決しないかもしれませんが、あなたの 'fields_for'ブロックに' f'変数がありません。これを 'ff'などの名前に変更して、' text_field'入力が 'chef'パラメータに割り当てられていることを確認してください。これにより、検証の一部が失敗し、フォームがリロードされる可能性があります。 – Shaun

+0

が完了しましたが、問題は解決していません。/ – imjustaguy

+0

フォームを送信するときに、developer.logファイルに受信パラメータが表示されますか?これは、次のようなものでなければなりません: 'POST"/url "for ...;パラメータ:{"restaurant" => {...}、 "シェフ" => {...}} '。 – Shaun

答えて

0

あなたの問題はthis questionに関連しているように見えます。 Rails 5では、belongs_toの関連付けに対して暗黙の検証が行われているため、ネストされた属性から構築するには、required: falseを追加する必要があります。

class Chef < ApplicationRecord 
    belongs_to :restaurant, required: false 
    ... 
end 
+0

ありがとうございますが で修正しましたhas_one:シェフ、 inverse_of::レストラン – imjustaguy