Note that there are some explanatory texts on larger screens.

plurals
  1. PORuby on Rails edit/update method is creating a new object instead of updating
    primarykey
    data
    text
    <p>I'm having a very similar problem (not quite sure if it's exactly the same) as this post:</p> <p><a href="https://stackoverflow.com/questions/1350364/edit-method-is-creating-new-records-instead-of-just-updating-existing-ones">Edit method is creating new records, instead of just updating existing ones</a></p> <p>^^As you may notice, there is no solution posted.</p> <p>I have a map set up for our building's network. It's setup as floors=>switches, switches=>jacks. Each are only nested once.</p> <p>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. 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.</p> <p>Here is relevant code. If you want to see anything else, let me know.</p> <p>...app/views/switches/_form.html.erb:</p> <pre><code>&lt;%= form_for([@floor, @switch]) do |f| %&gt; &lt;% if @switch.errors.any? %&gt; &lt;div id="error_explanation"&gt; &lt;h2&gt;&lt;%= pluralize(@switch.errors.count, "error") %&gt; prohibited this switch from being saved:&lt;/h2&gt; &lt;ul&gt; &lt;% @switch.errors.full_messages.each do |msg| %&gt; &lt;li&gt;&lt;%= msg %&gt;&lt;/li&gt; &lt;% end %&gt; &lt;/ul&gt; &lt;/div&gt; &lt;% end %&gt; &lt;div class="field"&gt; &lt;%= f.label :title %&gt;&lt;br /&gt; &lt;%= f.text_field :title %&gt; &lt;/div&gt; &lt;div class="actions"&gt; &lt;%= f.submit %&gt; &lt;/div&gt; &lt;% end %&gt; </code></pre> <p>...app/controllers/switches_controller.rb:</p> <pre><code>class SwitchesController &lt; 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 =&gt; @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 =&gt; 'Floor was successfully updated.' } format.json { head :no_content } else format.html { render :action =&gt; "edit" } format.json { render :json =&gt; @switch.errors, :status =&gt; :unprocessable_entity } end end end end </code></pre> <p>Can anyone help figure out why it's going to the create method instead of the update? Thanks!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload