Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 3 Rails在不同的控制器中更新_Ruby On Rails 3 - Fatal编程技术网

Ruby on rails 3 Rails在不同的控制器中更新

Ruby 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

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

记录的客户控制器.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
  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 |%> 表单包含。
... 我可以显示localhost:3000/视图,其中是编辑按钮。在localhost:3000/edit中,表单显示了自动填充的信息,一切看起来都很好。当我点击提交按钮时,我被重定向到相同的编辑自动填写表单,但没有任何错误?所以我想这是因为更新失败了,还有一个错误是它没有呈现错误。我做错了什么

我已经登录了customer\u controller.rb,因为customer\u controller.rb用于管理目的,并且处于授权之下

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

于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 |%>
谢谢,这很有效,我终于知道匹配和获取之间的区别:-)谢谢,这很有效,我终于知道匹配和获取之间的区别:-)