我正在关注教程:http://www.amooma.de/screencasts/2015-01-22-nested_forms-rails-4.2/
我正在使用Rails 5.0.0.1
但是当我注册酒店时,似乎酒店类别必须存在.
1错误禁止此酒店被保存:类别酒店必须存在
我的酒店型号:
class Hotel < ApplicationRecord
has_many :categories, dependent: :destroy
validates :name, presence: true
accepts_nested_attributes_for :categories, reject_if: proc { |attributes| attributes['name'].blank? }, allow_destroy: true
end
Run Code Online (Sandbox Code Playgroud)
我的分类型号:
class Category < ApplicationRecord
belongs_to :hotel
validates :name, presence: true
end
Run Code Online (Sandbox Code Playgroud)
我的酒店管理员:
def new
@hotel = Hotel.new
@hotel.categories.build
end
def hotel_params
params.require(:hotel).permit(:name, categories_attributes: [ :id,:name])
end
Run Code Online (Sandbox Code Playgroud)
结束我的_form.html.erb
<%= f.fields_for :categories do |category| %>
<div class="room_category_fields">
<div class="field">
<%= category.label :name %><br>
<%= category.text_field :name %>
</div>
</div> …Run Code Online (Sandbox Code Playgroud) 我尝试了以下但返回为空。
<?php
namespace App\Providers;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Route;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
class AuthServiceProvider extends ServiceProvider {
public function boot() {
$routeList = Route::getRoutes();
foreach ($routeList as $value) {
dd($value->getPath());
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的路线文件:
<?php
Route::namespace('admin')->group(function () {
Route::get('admin/post', 'PostController@index')->name('posts');
Route::get('admin/post/new', 'PostController@new')->name('post_new');
Route::post('admin/post/save', 'SubjectController@save')->name('post_save');
});
Run Code Online (Sandbox Code Playgroud)
我尝试了几种方法,但无法列出在 web.php 路由文件中创建的路由