简体   繁体   English

Ruby on Rails编辑/更新方法正在创建一个新对象,而不是更新

[英]Ruby on Rails edit/update method is creating a new object instead of updating

I'm having a very similar problem (not quite sure if it's exactly the same) as this post: 我有一个与此帖子非常相似的问题(不确定是否完全相同):

Edit method is creating new records, instead of just updating existing ones Edit方法正在创建新记录,而不仅仅是更新现有记录

^^As you may notice, there is no solution posted. ^^您可能会注意到,没有发布解决方案。

I have a map set up for our building's network. 我已经为建筑物的网络设置了地图。 It's setup as floors=>switches, switches=>jacks. 它设置为floor => switchs,switchs => jacks。 Each are only nested once. 每个仅嵌套一次。

The problem is in my switch edit/update methode. 问题出在我的开关编辑/更新方法中。 When I click on the edit button for a switch, it redirects me to the correct url (.../switch/1/edit), but I notice right away the form is incorrect. 当我单击开关的编辑按钮时,它会将我重定向到正确的URL(... / switch / 1 / edit),但我立即注意到该表单不正确。 Instead of the button saying "update switch", it says "create switch", and that's exactly what happens. 而不是按钮说“更新开关”,而是说“创建开关”,这就是实际发生的情况。 A new switch is created instead of the switch I wanted to update being updated. 将创建一个新的开关,而不是要更新的开关。

Here is relevant code. 这是相关的代码。 If you want to see anything else, let me know. 如果您想查看其他内容,请告诉我。

...app/views/switches/_form.html.erb: ... app / views / switches / _form.html.erb:

<%= form_for([@floor, @switch]) do |f| %>
  <% if @switch.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@switch.errors.count, "error") %> prohibited this switch from being saved:</h2>

      <ul>
      <% @switch.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :title %><br />
    <%= f.text_field :title %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

...app/controllers/switches_controller.rb: ... app / controllers / switches_controller.rb:

class SwitchesController < ApplicationController

  def create
    @floor = Floor.find(params[:floor_id])
    @switch = @floor.switches.create(params[:switch])
    redirect_to(@floor)
  end

  def destroy
    @floor = Floor.find(params[:floor_id])
    @switch = @floor.switches.find(params[:id])
    @switch.destroy
    redirect_to(@floor)
  end

  def show
    @floor = Floor.find(params[:floor_id])
    @switch = @floor.switches.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render :json => @switch }
    end
  end

  def edit
    @floor = Floor.find(params[:floor_id])
    @switch = @floor.switches.find(params[:id])
  end

  def update
    @floor = Floor.find(params[:id])
    @switch = @floor.switches.find(params[:id])

    respond_to do |format|
      if @switch.update_attributes(params[:switch])
        format.html { redirect_to @switch, :notice => 'Floor was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render :action => "edit" }
        format.json { render :json => @switch.errors, :status => :unprocessable_entity }
      end
    end
  end

end

Can anyone help figure out why it's going to the create method instead of the update? 谁能帮忙弄清楚为什么要使用create方法而不是update方法? Thanks! 谢谢!

The error is here, form is always built for new switch 错误在这里,表单总是为新开关构建的

<%= form_for([@floor, @floor.switches.build]) do |f| %>
  <% @switch = @floor.switches.find(params[:id]) %>

Change it to 更改为

<%= form_for([@floor, @switch]) do |f| %>

( and remove line ) (并删除行)

<% @switch = @floor.switches.find(params[:id]) %>

I don't see new action in the controller, it can looks like 我在控制器中看不到new动作,看起来像

def new
  @floor = Floor.find(params[:floor_id])
  @switch = @floor.switches.build
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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