[英]rails- updating form redirecting to the wrong page
I have a form to update the user's details, however, they are called customers and have a customers controller. 我有一个表格来更新用户的详细信息,但是,它们被称为客户并具有客户控制者。 Making changes and updating the form works but it redirects to /user/1 instead of /customer/1/
进行更改和更新表单是可行的,但是它重定向到/ user / 1而不是/ customer / 1 /
Form, should this be @user or @customer ? 表格,应为@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>
Controller 调节器
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
Routes 路线
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
This is probably really simple but I can't seem to work it out. 这可能真的很简单,但我似乎无法解决。 Thanks for your help.
谢谢你的帮助。
<%= form_for(@user) do |f| %>
This line sends the form to UsersController if the @user hasn't yet an id then it'll be send it to create method, if the @user has an id which means it already exists at db then it'll be send it to update method 如果@user还没有ID,则此行将表单发送给UsersController,然后将其发送给createMethod;如果@user具有ID意味着它已经存在于db,则将其发送给UsersController更新方法
The answer to your question yes you should create a form_for @customer if what you want is send the form CustomersController 问题的答案是,如果您要发送的是窗体CustomerController,则应创建一个form_for @customer
<%= form_for(@customer) do |f| %>
NOTE: @customer must exist at your new method 注意:@customer必须存在于您的新方法中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.