Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.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 rails-更新表单重定向到错误页面_Ruby On Rails_Ruby On Rails 4_Devise - Fatal编程技术网

Ruby on rails rails-更新表单重定向到错误页面

Ruby on rails rails-更新表单重定向到错误页面,ruby-on-rails,ruby-on-rails-4,devise,Ruby On Rails,Ruby On Rails 4,Devise,我有一个表单来更新用户的详细信息,但是,他们被称为客户,并且有一个客户控制器。 进行更改和更新表单是可行的,但它会重定向到/user/1而不是/customer/1/ 表单,这应该是@user还是@customer <divclass="form-group"> <%= form_for(@user) do |f| %> <% if @user.errors.any? %> <div id="error_explanation">

我有一个表单来更新用户的详细信息,但是,他们被称为客户,并且有一个客户控制器。 进行更改和更新表单是可行的,但它会重定向到/user/1而不是/customer/1/

表单,这应该是@user还是@customer

 <divclass="form-group">
<%= form_for(@user) do |f| %>
  <% if @user.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@customer.errors.count, "error") %> prohibited this category from being saved:</h2>

      <ul>
      <% @customer.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

<form>
  <div class="form-group">
    <%= f.label :firstname, "Name:", class: "col-md-4 control-label" %>
    </br>
    <%= f.text_field :firstname, class: "form-control"  %>
  </div>
   <div class="form-group">
    <%= f.label :phone1, "Phone:", class: "col-md-4 control-label" %>
    </br>
 <%= f.text_field :phone1, class: "form-control" %>

  </div>

  <div class="actions">
    <%= f.submit %>
<% end %>

</div>
这可能真的很简单,但我似乎无法解决它。 谢谢你的帮助


<%= form_for(@user) do |f| %>
此行将表单发送给UsersController如果@user还没有id,那么它将被发送给create方法,如果@user有一个id,这意味着它已经存在于db中,那么它将被发送给update方法

您问题的答案是“是”,您应该为@customer创建一个表单,如果您想发送表单CustomersController

class CustomersController < ApplicationController
  before_action :set_user, only: [:show, :edit, :update, :destroy]
  # match '/customers/:id', :to => 'users#show', :as => :user

  def index
    @users = User.all
  end

  def show
    @users = User.where(id: params[:id])
  end


  def new
      @users = User.new
  end

  # GET /categories/1/edit
  def edit

  end

  # POST /categories
  # POST /categories.json
   def create
    @user = User.new(user_params)

    respond_to do |format|
      if @user.save
        format.html { redirect_to @user, notice: 'user was successfully created.' }
        format.json { render :show, status: :created, location: @driver }
      else
        format.html { render :new }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end


  def update
    respond_to do |format|
      if @customer.update(user_params)
        format.html { redirect_to @customer, notice: 'User was successfully updated.' }
        format.json { render :show, status: :ok, location: @customer }
      else
        format.html { render :edit }
        format.json { render json: @customer.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /categories/1
  # DELETE /categories/1.json
  def destroy
    @user.destroy
    respond_to do |format|
      format.html { redirect_to user_url, notice: 'User was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_user
      @user = User.find(params[:id])
    end
    def set_customer
      @customer = User.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def user_params
      params.require(:user).permit(:title, :firstname, :lastname, :billing_address, :billing_postcode, :delivery_address, :delivery_postcode, :phone1, :phone2, :notes)
    end

end
    <%= form_for(@customer) do |f| %>


注意:@customer必须存在于您的新方法中

我仍然无法使其工作。我可以为任何/user/1设置一个指向customer/1的重定向吗?从长远来看,这会很糟糕吗?在您的customers\u controller.rb上,您创建的用户毫无意义。如果要将数据发送到CustomerController,请为customer创建一个表单。
    <%= form_for(@customer) do |f| %>