如何将编辑表单路由到控制器中的更新操作? (Rails app)

时间:2016-03-10 05:21:37

标签: ruby-on-rails forms controller

我正在为定制西装业务构建应用程序。该应用程序存储用户测量值,并为他们提供编辑这些测量值和任何订单开始的机会。编辑表单看起来很好,但如果我点击提交,我会得到"未知操作 - 操作'更新'找不到MeasurementsController。"但是,我在该控制器中有更新操作。出了什么问题?

控制器代码

class MeasurementsController < ApplicationController
def new
  @measurement = Measurement.new
end

def index
  @measurement = Measurement.find(params[:id])
end

def create
  @measurement = Measurement.new(safe_params)
  @measurement.user_id = current_user.id
  @measurement.save
  redirect_to measurements_path(id: @measurement.id)
end

def edit
  @measurement = Measurement.find_by(user_id: current_user.id)
end

def udpate
  @measurement = Measurement.find_by(user_id: current_user.id)
  if @measurement.update_attributes(safe_params)
    redirect_to new_order_path #to be changed later
  else
    redirect_to root_path #to be changed later
  end 
end

private

def safe_params
  params.require(:measurement).permit(:shoulders, :sleeve, :chest, :stomach, :jacket_length, :front, :back, :waist, :hips, :crotch, :pants_length, :thigh, :knee, :ankle)
end
end

部分编辑表格

<%= form_for(@measurement, :id => @measurement.id) do |f| %>
<table>
    <% @measurement.attributes.each do |name, value| %>
        <tr>
            <% if name != 'created_at' && name != 'id' && name != 'updated_at' && name != 'user_id' && name != 'order_id' %>
                <td><%= f.label name, id: "measurement-label"%></td>
                <td id="measurement-input"><%= f.number_field name, :step => 'any', class: "form-control" %></td>
            <% end %>
        </tr>
    <% end %>
</table>

<%= f.submit "Submit", class: "btn btn-primary" %>

为表单生成的HTML

<div class="container">
</div>

<div class="container">
    <h1></h1>
        <h2>Would you like to update your measurements?</h2>
    <form class="edit_measurement" id="edit_measurement_5" action="/measurements/5" accept-charset="UTF-8" method="post"><input name="utf8" type="hidden" value="✓"><input type="hidden" name="_method" value="patch"><input type="hidden" name="authenticity_token" value="IYs39JMpzNqLvQOVISsenOj1gy3QaPklIhvP0xVd2SCLHTw8FHjTi9iTlmK8R7xHsauu3Rvfh8QasvbVsbcBVw==">
<table>
        <tbody><tr>
        </tr>
        <tr>
                <td><label id="measurement-label" for="measurement_shoulders">Shoulders</label></td>
                <td id="measurement-input"><input step="any" class="form-control" type="number" value="9.0" name="measurement[shoulders]" id="measurement_shoulders"></td>
        </tr>
        <tr>
                <td><label id="measurement-label" for="measurement_sleeve">Sleeve</label></td>
                <td id="measurement-input"><input step="any" class="form-control" type="number" value="9.0" name="measurement[sleeve]" id="measurement_sleeve"></td>
        </tr>
        <tr>
                <td><label id="measurement-label" for="measurement_chest">Chest</label></td>
                <td id="measurement-input"><input step="any" class="form-control" type="number" value="9.0" name="measurement[chest]" id="measurement_chest"></td>
        </tr>
        <tr>
                <td><label id="measurement-label" for="measurement_stomach">Stomach</label></td>
                <td id="measurement-input"><input step="any" class="form-control" type="number" value="9.0" name="measurement[stomach]" id="measurement_stomach"></td>
        </tr>
        <tr>
                <td><label id="measurement-label" for="measurement_jacket_length">Jacket length</label></td>
                <td id="measurement-input"><input step="any" class="form-control" type="number" value="9.0" name="measurement[jacket_length]" id="measurement_jacket_length"></td>
        </tr>
        <tr>
                <td><label id="measurement-label" for="measurement_front">Front</label></td>
                <td id="measurement-input"><input step="any" class="form-control" type="number" value="9.0" name="measurement[front]" id="measurement_front"></td>
        </tr>
        <tr>
                <td><label id="measurement-label" for="measurement_back">Back</label></td>
                <td id="measurement-input"><input step="any" class="form-control" type="number" value="9.0" name="measurement[back]" id="measurement_back"></td>
        </tr>
        <tr>
                <td><label id="measurement-label" for="measurement_waist">Waist</label></td>
                <td id="measurement-input"><input step="any" class="form-control" type="number" value="9.0" name="measurement[waist]" id="measurement_waist"></td>
        </tr>
        <tr>
                <td><label id="measurement-label" for="measurement_hips">Hips</label></td>
                <td id="measurement-input"><input step="any" class="form-control" type="number" value="9.0" name="measurement[hips]" id="measurement_hips"></td>
        </tr>
        <tr>
                <td><label id="measurement-label" for="measurement_crotch">Crotch</label></td>
                <td id="measurement-input"><input step="any" class="form-control" type="number" value="9.0" name="measurement[crotch]" id="measurement_crotch"></td>
        </tr>
        <tr>
                <td><label id="measurement-label" for="measurement_pants_length">Pants length</label></td>
                <td id="measurement-input"><input step="any" class="form-control" type="number" value="9.0" name="measurement[pants_length]" id="measurement_pants_length"></td>
        </tr>
        <tr>
                <td><label id="measurement-label" for="measurement_thigh">Thigh</label></td>
                <td id="measurement-input"><input step="any" class="form-control" type="number" value="9.0" name="measurement[thigh]" id="measurement_thigh"></td>
        </tr>
        <tr>
                <td><label id="measurement-label" for="measurement_knee">Knee</label></td>
                <td id="measurement-input"><input step="any" class="form-control" type="number" value="9.0" name="measurement[knee]" id="measurement_knee"></td>
        </tr>
        <tr>
                <td><label id="measurement-label" for="measurement_ankle">Ankle</label></td>
                <td id="measurement-input"><input step="any" class="form-control" type="number" value="9.0" name="measurement[ankle]" id="measurement_ankle"></td>
        </tr>
        <tr>
        </tr>
        <tr>
        </tr>
        <tr>
        </tr>
        <tr>
        </tr>
</tbody></table>

<input type="submit" name="commit" value="Submit" class="btn btn-primary">

1 个答案:

答案 0 :(得分:4)

查看您的方法update这是错误的:

而不是udpate更改为update。您的上述方法为udpateudpate的方法不应为update

上面的代码是(方法名称udpate错误):

def udpate
  @measurement = Measurement.find_by(user_id: current_user.id)
  if @measurement.update_attributes(safe_params)
    redirect_to new_order_path #to be changed later
  else
    redirect_to root_path #to be changed later
 end 
end

更改为:

def update
  @measurement = Measurement.find_by(user_id: current_user.id)
  if @measurement.update_attributes(safe_params)
    redirect_to new_order_path #to be changed later
  else
    redirect_to root_path #to be changed later
 end 
end