在尝试添加到表

时间:2017-04-26 11:23:47

标签: ruby-on-rails activerecord

我正在尝试通过employee的显示页面上的employee_skill表格向员工添加技能,但每次尝试创建时都会收到以下错误。

错误:

ActiveRecord::RecordNotFound in EmployeeSkillsController#create

Couldn't find Employee with 'id'=

员工秀

<div class="section">
<div class="top-border left"></div>
  <div class="top-border right"></div>
    <h3> Add Skill</h3>
    <%= form_for([EmployeeSkill.new]) do |f| %>
    <%= @employee.name%>
<div class="field">
     <%= f.label :Skill %><br>
    <%= f.collection_select :skill_id, Skill.all, :id, :skillType, :prompt => "Select skill" %>
  </div>

  <div class="actions">
  <%= f.label:skillLevel%>
    <% stars = [1,2,3,4,5] %>
    <% stars.each do |star| %>
        <%= f.radio_button :skillLevel,star%> <%= star%>
    <%end%>
    </br>
    <%= button_to "Add Skill", employee_skills_path(employee_id: @employee.id), class: "btn btn-primary" %> 
    </form>
</div>
<%end%>

员工技能总监

  # POST /employee_skills
  # POST /employee_skills.json
  def create

    @employee_skill = EmployeeSkill.new
    @employee = Employee.find(params[:employee_id]) 
    @employee_skill.employee_id= @employee.id


    respond_to do |format|
      if @employee_skill.save
        #session[:employee_id] = nil
        format.html { redirect_to @employee, notice: 'Employee skill was successfully created.' }
        format.json { render :show, status: :created, location: @employee_skill }
      else
        format.html { render :new }
        format.json { render json: @employee_skill.errors, status: :unprocessable_entity }
      end
    end
  end

更新

日志文件

Started POST "/employee_skills" for ::1 at 2017-04-26 12:35:38 +0100
Processing by EmployeeSkillsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"bJ/wgo1Iz7KqLDtijEZ/emuWTuS4UblMlVnz7SyBZUSR7MJlxAmtXk5LS661t6598WfnCycDf6LtUHwhTOf9hQ==", "employee_skill"=>{"skill_id"=>"1", "skillLevel"=>"3"}}
  [1m[35mEmployee Load (0.5ms)[0m  SELECT  "employees".* FROM "employees" WHERE "employees"."id" = ? LIMIT 1  [["id", 1]]
  [1m[36mEmployee Load (1.0ms)[0m  [1mSELECT  "employees".* FROM "employees" WHERE "employees"."id" = ? LIMIT 1[0m  [["id", nil]]
Completed 404 Not Found in 22ms (ActiveRecord: 1.5ms)

ActiveRecord::RecordNotFound (Couldn't find Employee with 'id'=):
  app/controllers/employee_skills_controller.rb:31:in `create'


  Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/actionpack-4.2.6/lib/action_dispatch/middleware/templates/rescues/_source.erb (1.5ms)
  Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/actionpack-4.2.6/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (12.0ms)
  Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/actionpack-4.2.6/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (5.0ms)
  Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/actionpack-4.2.6/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (292.9ms)
  Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/web-console-2.3.0/lib/web_console/templates/_markup.html.erb (2.0ms)
  Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/web-console-2.3.0/lib/web_console/templates/_inner_console_markup.html.erb within layouts/inlined_string (1.5ms)
  Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/web-console-2.3.0/lib/web_console/templates/_prompt_box_markup.html.erb within layouts/inlined_string (2.0ms)
  Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/web-console-2.3.0/lib/web_console/templates/style.css.erb within layouts/inlined_string (1.5ms)
  Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/web-console-2.3.0/lib/web_console/templates/console.js.erb within layouts/javascript (485.7ms)
  Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/web-console-2.3.0/lib/web_console/templates/main.js.erb within layouts/javascript (1.0ms)
  Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/web-console-2.3.0/lib/web_console/templates/error_page.js.erb within layouts/javascript (3.0ms)
  Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/web-console-2.3.0/lib/web_console/templates/index.html.erb (885.4ms)

1 个答案:

答案 0 :(得分:1)

EmployeeSkill操作

中初始化show的实例
@employee = Employee.find(params[:id]) 
@employee_skill = @employee.employee_skills.new
# The above statement will set :employee_id with employee's id

然后以表格

使用它
<%= form_for([@employee, @employee_skill]) do |f| %>

如果:url生成不正确,您也可以明确传递

<%= form_for([@employee, @employee_skill], url: employee_employee_skills_path(@employee)) do |f| %>