0
私はユーザーの詳細を更新するフォームを持っていますが、顧客と呼ばれ、顧客コントローラを持っています。 フォームの変更と更新は機能しますが、/ customer/1/rails-更新フォーム間違ったページへのリダイレクト
の代わりに/ user/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を指すように/ user/1のリダイレクトを設定することができますか?これは長期的には悪いですか? – Walsh259
をcustomers_controller.rbに作成すると、意味をなさないユーザーが作成されます。データをCustomerControllerに送信する場合は、form_forカスタマーを作成します。 –