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

时间:2017-09-18 19:35:33

标签: 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">
      <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>

控制器

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

路线

Rails.application.routes.draw do



    get 'customers/index'
    get 'customers/new'
    get 'customers/delete'
    get 'customers/edit'

  end



  resources :vans
  resources :customers do
  # resources :orders
end
  devise_for :users
  # Change get method for user signout
  devise_scope :user do
    get '/users/sign_out' => 'devise/sessions#destroy'
  end



  root 'pages#index'

  resources :categories
  resources :products, only: [:show]
  resources :drivers
  resources :product_items
  resources :baskets
  resources :orders
  resources :customers
  resources :taxes
  resources :users

这可能很简单,但我似乎无法解决这个问题。 谢谢你的帮助。

1 个答案:

答案 0 :(得分:1)

<%= form_for(@user) do |f| %>

如果@user还没有id,那么该行将表单发送给UsersController,如果@user有一个id,这意味着它已经存在于db那么它将被发送到create方法它会将其发送给更新方法

你的问题的答案是的,你应该创建一个form_for @customer,如果你想要的是发送表单CustomersController

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

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