简体   繁体   中英

Relationship models in rails

Im associating models and im stuck i have my Restaurant controller

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

And this is my _form that im rendering in 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 %>

then this is my Chef model

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

and this is my Restaurant model

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

My problem is that when i save a new restauran, nothing happens just reloads and stay in the form, i dont know whats happening im stuck here, probably its just a dumb thing to do but im learning, thanks for the support.

日志记录

It looks like your problem is related to this question . In Rails 5, there is an implicit validation on belongs_to associations, so you need to add required: false in order to build it from nested attributes.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM