Ruby on rails 3 Rails在不同的控制器中更新
你好,我正在做简单的更新 记录的客户控制器.rbRuby on rails 3 Rails在不同的控制器中更新,ruby-on-rails-3,Ruby On Rails 3,你好,我正在做简单的更新 记录的客户控制器.rb class LoggedCustomerController < ApplicationController before_filter :authorize helper_method :current_customer layout "frontend" def current_customer @current_customer ||= Customer.find(session[:customer_id
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
match "view" => "logged_customer#show", :via => :get
match "edit" => "logged_customer#edit", :via => :get
match "edit" => "logged_customer#edit", :via => :put
<%= 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 %>
...
edit.html.erb
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
match "view" => "logged_customer#show", :via => :get
match "edit" => "logged_customer#edit", :via => :get
match "edit" => "logged_customer#edit", :via => :put
<%= 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 %>
...
(:controller=>'logged_customer',:action=>'edit'),:html=>{:class=>'form horizontal'}do | f |%>
表单包含。
于2013-08-19 14:54:30+0200开始对127.0.0.1进行“编辑”
LoggedCustomerController处理#编辑为HTML
参数:{…}
在布局/前端(67.0ms)内呈现日志记录的用户/u customer/edit.html.erb
在89毫秒内完成200 OK(视图:33.0毫秒|活动记录:56.0毫秒)
好吧,在你的表单上,你说这个动作是编辑
动作,而这个动作应该是更新
<%= form_for current_customer,
:url => url_for(:controller => 'logged_customer', :action => 'edit'),
:html => { :class => 'form-horizontal' } do |f| %>
嗯,在你的表单上,你说这个动作是编辑
动作,而这个动作应该是更新
<%= form_for current_customer,
:url => url_for(:controller => 'logged_customer', :action => 'edit'),
:html => { :class => 'form-horizontal' } do |f| %>
更改路线(rails 3):
更改路线(rails 4):
更改您的表格:
<%= form_for current_customer, :url => url_for(:controller => 'logged_customer', :action => 'update'), :method => "patch", :html => { :class => 'form-horizontal' } do |f| %>
<%= # your form fields %>
<% end %>
(:controller=>‘logged_customer’,:action=>‘update’,:method=>‘patch’,:html=>{:class=>‘form horizontal’}do | f |%>
更改路线(rails 3):
更改路线(rails 4):
更改您的表格:
<%= form_for current_customer, :url => url_for(:controller => 'logged_customer', :action => 'update'), :method => "patch", :html => { :class => 'form-horizontal' } do |f| %>
<%= # your form fields %>
<% end %>
(:controller=>‘logged_customer’,:action=>‘update’,:method=>‘patch’,:html=>{:class=>‘form horizontal’}do | f |%>
谢谢,这很有效,我终于知道匹配和获取之间的区别:-)谢谢,这很有效,我终于知道匹配和获取之间的区别:-)