我的表单存在问题,因为在输入数据后,调用show方法时会得到Couldn't find Employeur without an ID
。我认为这是由于我的嵌套资源。
以下是我的路线:
resources :users do
resource: employeur
resource: prestataire
end
我的用户控制器,一旦用户填写了表格,就会将其发送给我们:
class UsersController < ApplicationController
#TODO index user doit être suprimé quand inutile pour dev
def index
@users = User.all
end
def show
@user = User.find(params[:id])
end
def new
@user = User.new
end
# GET /users/1/edit
def edit
@user = User.find(params[:id])
end
# POST /users
# POST /users.json
def create
@user = User.new(user_params)
respond_to do |format|
if @user.save
if params[:commit] == 'Prestataire'
format.html { redirect_to new_user_prestataire_path(user_id: @user), notice: "Renseignez vos informations d'employeur" }
format.json { render action: 'show', status: :created, location: @user }
else
format.html { redirect_to new_user_employeur_path(user_id: @user), notice: "Renseignez vos informations de prestataire" }
format.json { render action: 'show', status: :created, location: @user }
end
else
format.html { render action: 'new' }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /users/1
# PATCH/PUT /users/1.json
def update
@user = User.find(params[:id])
respond_to do |format|
if @user.update(user_params)
if params[:commit] == 'Prestataire'
format.html { redirect_to new_user_prestataire_path(user_id: @user), notice: 'User was successfully updated.' }
format.json { head :no_content }
else
format.html { redirect_to new_user_employeur_path(user_id: @user), notice: "User was successfully updated." }
format.json { head :no_content }
end
else
format.html { render action: 'edit' }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
# DELETE /users/1
# DELETE /users/1.json
def destroy
@user = User.find(params[:id])
@user.destroy
respond_to do |format|
format.html { redirect_to users_url }
format.json { head :no_content }
end
end
private
def user_params
params.require(:user).permit(:email, :password, :password_confirmation, :surname, :forename, :civility, :phone)
end
end
我遇到麻烦的雇员表格:
<%= form_for [@user, @employeur] do |f| %>
<% if @employeur.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@employeur.errors.count, "error") %> prohibited this employeur from being saved:</h2>
<ul>
<% @employeur.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :siren, 'Siren: ' %><br>
<%= f.text_field :siren %>
</div>
<div class="field">
<%= f.label :societe, 'Société: ' %><br>
<%= f.text_field :societe %>
</div>
<div class="field">
<%= f.label :code_postal, 'Code Postal: ' %><br>
<%= f.text_field :code_postal %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
我的雇员控制器找不到我的雇员参数:
class EmployeursController < ApplicationController
before_filter :load_user
def index
@employeurs = @user.employeur.all
end
def show
@employeur = Employeur.find(params[:id])
end
def new
@employeur = @user.build_employeur
end
def edit
@employeur = Employeur.find(params[:id])
end
def create
@employeur = @user.build_employeur(employeur_params)
respond_to do |format|
if @employeur.save
format.html { redirect_to [@user, @employeur], notice: 'Employeur was successfully created.' }
format.json { render action: 'show', status: :created, location: @employeur }
else
format.html { render action: 'new' }
format.json { render json: @employeur.errors, status: :unprocessable_entity }
end
end
end
def update
@employeur = Employeur.find(params[:id])
respond_to do |format|
if @employeur.update_attributes(employeur_params)
format.html { redirect_to [@user, @employeur], notice: 'Employeur was successfully created.' }
format.json { render action: 'show', status: :created, location: @employeur }
else
format.html { render action: 'new' }
format.json { render json: @employeur.errors, status: :unprocessable_entity }
end
end
end
def destroy
@employeur = Employeur.find(params[:id])
@employeur.destroy
respond_to do |format|
format.html { redirect_to @user }
format.json { head :no_content }
end
end
private
def load_user
@user = User.find(params[:user_id])
end
def employeur_params
params.require(:employeur).permit(:siren, :societe, :code_postal)
end
end
我决定编辑我的第一篇文章,添加与@Sirius ROR的雇员相关的所有路线:
user_employeur POST /users/:user_id/employeur(.:format) em
ployeurs#创建 new_user_employeur GET /users/:user_id/employeur/new(.:format)em ployeurs#新 edit_user_employeur GET /users/:user_id/employeur/edit(.:format)em ployeurs#编辑 GET /users/:user_id/employeur(.:format)em ployeurs#秀 PATCH /users/:user_id/employeur(.:format)em ployeurs#更新 PUT /users/:user_id/employeur(.:format)em ployeurs#更新 删除/users/:user_id/employeur(.:format)em ployeurs#破坏
我也做了一个重大改变:
变成了:因为我对我的雇主模型做了重大改变:
class Employeur < ActiveRecord::Base
model_name.instance_variable_set :@route_key, 'employeur'
belongs_to :user
has_many :projets, as: :projetable
has_many :prestataires, through: :projets
has_many :offres, through: :projets
has_many :feedbacks, through: :projets
validates :siren, :societe, :code_postal, presence: true
end
根据我最终在互联网上找到的内容,第一行涉及网址问题。尽管有这样的改变,我仍然得到完全相同的错误:找不到没有ID的Employeur。
这也是我的用户模型:
class User < ActiveRecord::Base
has_one :prestataire
has_one :employeur
accepts_nested_attributes_for :employeur, allow_destroy: true
has_secure_password
end
答案 0 :(得分:1)
尝试从form_for中删除url选项作为表单,以自动构建帖子网址
<%= form_for [@user, @employeur] do |f| %>
这一定很有效。