2013-08-19 56 views
1

您好即時通訊做簡單的更新..Rails的更新

logged_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 

的routes.rb

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

edit.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其中是編輯按鈕。在本地主機:3000 /編輯窗體顯示自動填充信息,一切看起來不錯。當我點擊提交按鈕,我重定向到相同的編輯自動填寫表單,但沒有任何錯誤?所以我想有一些錯誤,因爲更新失敗,另一個錯誤,它不會呈現錯誤。什麼即時做錯了?

我有logged_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) 

回答

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 
+1

這些路線也不正確。 – Mattherick

+0

非常感謝你的工作:-) – Muflix

2

更改你的路由(軌道3):

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

更改路線(軌道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 %> 
+0

謝謝你的作品,最後我知道匹配和區別:-) – Muflix