无法确定如何设置此表单。我有两个模型Business和User。商家有belong_to :business
,用户有Rails.application.routes.draw do
devise_for :users, :controllers => { :registrations => 'registrations' }
root 'pages#index'
resources :businesses do
resources :users
end
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
。我试图创建一个初始表单来创建一个企业和第一个用户(所有者)。这是我的路线文件:
class Business < ApplicationRecord
has_many :users
accepts_nested_attributes_for :users
end
business.rb
class User < ApplicationRecord
rolify
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
belongs_to :business
end
user.rb
<div class="row center-xs">
<div class="col-sm-6">
<h1>New Business</h1>
<%= simple_form_for @business do |b| %>
<div class="mdl-textfield mdl-js-textfield">
<%= b.input :name, label_html: {class: 'mdl-textfield__label'}, input_html: {class: 'mdl-textfield__input'}, required: true, label: "Business Name" %>
</div><br>
<%= b.simple_fields_for(resource, :as => resource_name, :url => registration_path(resource_name), validate: true) do |f| %>
<%= devise_error_messages! %>
<%= f.input :role, as: :hidden, input_html: { value: "owner" } %>
<div class="mdl-textfield mdl-js-textfield">
<%= f.input :name, label_html: {class: 'mdl-textfield__label'}, input_html: {class: 'mdl-textfield__input'}, required: true, label: "Your Name" %>
</div><br>
<div class="mdl-textfield mdl-js-textfield">
<%= f.input :email, label_html: {class: 'mdl-textfield__label'}, input_html: {class: 'mdl-textfield__input'}, validate: true, required: true %>
</div><br>
<% if @minimum_password_length %>
<em>(<%= @minimum_password_length %> characters minimum)</em>
<% end %>
<div class="mdl-textfield mdl-js-textfield">
<%= f.input :password, label_html: {class: 'mdl-textfield__label'}, input_html: {class: 'mdl-textfield__input'}, autocomplete: "off", required: true %>
</div><br>
<div class="mdl-textfield mdl-js-textfield">
<%= f.input :password_confirmation, label_html: {class: 'mdl-textfield__label'}, input_html: {class: 'mdl-textfield__input'}, autocomplete: "off", required: true %>
</div><br>
<% end %>
<%= b.button :submit, "Sign up", class: 'mdl-button' %>
<% end %>
</div>
</div>
以下是商业路线的new.html.erb:
ActiveModel::UnknownAttributeError (unknown attribute 'user' for Business.)
当我尝试创建业务时,我收到一条错误消息:
def create
@business = Business.new(business_params)
respond_to do |format|
if @business.save
format.html { redirect_to @business, notice: 'Business was successfully created.' }
format.json { render :show, status: :created, location: @business }
else
format.html { render :new }
format.json { render json: @business.errors, status: :unprocessable_entity }
end
end
end
# Never trust parameters from the scary internet, only allow the white list through.
def business_params
params.require(:business).permit(:name, :user => [:email, :name, :password, :password_confirmation])
end
现在控制器只是脚手架生成的默认控制器:
public class TestRestHeartUpdate{
public static void main(String[] args) throws Exception{
URL url;
url = new
URL("http://127.0.0.1:8080/testDB1/col1/5967c13e61f73b88018db783");
HttpURLConnection hc;
hc = (HttpURLConnection) url.openConnection();
hc.setDoInput(true);
hc.setDoOutput(true);
hc.setRequestMethod("POST");
hc.setRequestProperty("X-HTTP-Method-Override", "PATCH");
hc.setRequestProperty("Content-Type","application/hal+json");
hc.setRequestProperty("Accept","application/hal+json");
hc.setInstanceFollowRedirects(false);
hc.setRequestProperty("If-Match","5967c28f42483b1c3dbedc54");
hc.setUseCaches(false);
hc.connect();
byte[] opB = " {'rating':'VeryGood'}".getBytes("UTF-8");
OutputStream os = hc.getOutputStream();
os.write(opB);
System.out.println(hc.getResponseCode());
}
}
答案 0 :(得分:0)
在业务控制器中重命名:permit函数中的用户密钥:users_attributes
params.require(:business).permit(:name, :users_attributes => [:email, :name, :password, :password_confirmation])