您好我正在进行简单的更新..
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其中是编辑按钮。在localhost: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)
答案 0 :(得分: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 :(得分: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 %>