1

你好我正在做简单的更新..

登录的_customer_controller.rb

class LoggedCustomerController < ApplicationController

  before_filter :authorize
  helper_method :current_customer

  layout "frontend"

  def current_customer
    @current_customer ||= Customer.find(session[:customer_id]) if session[:customer_id]
  end

  def authorize
    if session[:auth] != true
      redirect_to login_path, :notice => "Not logged."
    end
  end

  def show
  end

  def edit
  end

  def update
    respond_to do |format|
      if current_customer.update_attributes(params[:current_customer])
        format.html { redirect_to view_path, notice: 'Customer was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: current_customer.errors, status: :unprocessable_entity }
      end
    end
  end

end

路线.rb

  match "view" => "logged_customer#show", :via => :get
  match "edit" => "logged_customer#edit", :via => :get
  match "edit" => "logged_customer#edit", :via => :put

编辑.html.erb

    <%= form_for current_customer, :url => url_for(:controller => 'logged_customer', :action => 'edit'), :html => { :class => 'form-horizontal' } do |f| %>

<% if current_customer.errors.any? %>
  <div id="error_explanation">
    <div class="alert alert-error">
      The form contains <%= pluralize(current_customer.errors.count, "error") %>.
    </div>
    <ul>
    <% current_customer.errors.full_messages.each do |msg| %>
      <li> <%= msg %></li>
    <% end %>
    </ul>
  </div>
<% end %>

...

我可以显示 localhost:3000/view 编辑按钮在哪里。在 localhost:3000/edit 表单显示有自动填充的信息,一切看起来都很好。当我单击提交按钮时,我重定向到相同的编辑自动填充表单但没有任何错误?所以我猜有一些错误,因为更新失败和另一个错误,它不呈现错误。我做错了什么?

我已经记录了_customer_controller.rb,因为customer_controller.rb 用于管理目的并且已获得授权。

在 Development.log 我只有(看起来不错)

Started PUT "/edit" for 127.0.0.1 at 2013-08-19 14:54:30 +0200
Processing by LoggedCustomerController#edit as HTML
  Parameters: {...}
  <Executing SQL ...>
  Rendered logged_customer/edit.html.erb within layouts/frontend (67.0ms)
Completed 200 OK in 89ms (Views: 33.0ms | ActiveRecord: 56.0ms)
4

2 回答 2

2

好吧,form_for你说行动是edit一个,当它应该是update

<%= form_for current_customer, 
  :url => url_for(:controller => 'logged_customer', :action => 'edit'), 
  :html => { :class => 'form-horizontal' } do |f| %>

这样,当您提交时,它会点击编辑操作。改变这个,你很高兴。

另外,像@Mattherick 所说的那样改变你的路线:

match "update" => "logged_customer#update", :via => :put
于 2013-08-19T13:38:56.553 回答
2

改变你的路线(rails 3):

match "view" => "logged_customer#show", :via => :get
match "edit" => "logged_customer#edit", :via => :get
match "update" => "logged_customer#update", :via => :put

改变你的路线(rails 4):

get "view" => "logged_customer#show"
get "edit" => "logged_customer#edit"
patch "update" => "logged_customer#update"

更改您的表格:

<%= form_for current_customer, :url => url_for(:controller => 'logged_customer', :action => 'update'), :method => "patch", :html => { :class => 'form-horizontal' } do |f| %>
  <%= # your form fields %>
<% end %>
于 2013-08-19T13:53:31.580 回答