我是Ruby on Rails的新手。最近我创建了一个项目并添加一些宝石来支持我的应用程序。但是几个问题开始发生了。我创建了两个使用相同模型的Devise的新路由。然后,应用程序不使用正确的路由到操作。要使用EDIT操作,应用程序将重定向到CREATE操作。是什么导致了这个问题?关注我的文件:
我的routes.rb
Rails.application.routes.draw do
as :member do
resource :registration, only: [:create], :path => '/members'
end
resources :members_admin, :controller => 'members'
devise_for :members, :controllers => { :registrations => 'registrations'}
resources :managments
resources :expenses
resources :incomings
resources :members
resources :registrations
get 'home/index'
root to:'home#index'
# patch 'members_admin' => "members#update", :as => "members/update"
end
端
form.html.erb
<%= form_for @member,url: members_admin_index_path, :html => { :class => "form-horizontal member", :method => :post } do |f| %>
<% if @member.errors.any? %>
<div id="error_expl" class="panel panel-danger">
<div class="panel-heading">
<h3 class="panel-title"><%= pluralize(@member.errors.count, "erro") %> impediu(ram) que fosse criado:</h3>
</div>
<div class="panel-body">
<ul>
<% @member.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
</div>
<% end %>
<div class="form-group">
<%= f.label :name, :class => 'control-label col-lg-2' %>
<div class="col-lg-6">
<%= f.text_field :name,autofocus: true, :class => 'form-control' %>
</div>
<%=f.error_span(:name) %>
</div>
<div class="form-group">
<%= f.label :email, :class => 'control-label col-lg-2' %>
<div class="col-lg-6">
<%= f.email_field :email, :class => 'form-control' %>
</div>
<%=f.error_span(:email) %>
</div>
<div class="form-group">
<%= f.label :password, :class => 'control-label col-lg-2' %>
<% if @minimum_password_length %>
<em>(<%= @minimum_password_length %> characters minimum)</em>
<% end %>
<div class="col-lg-4">
<%= f.password_field :password, autocomplete: "off", :class => 'form-control'%>
</div>
<%=f.error_span(:password) %>
</div>
<div class="form-group">
<%= f.label :password_confirmation, :class => 'control-label col-lg-2' %>
<% if @minimum_password_length %>
<em>(Insira no mínimo <%= @minimum_password_length %> caracteres.)</em>
<% end %>
<div class="col-lg-4">
<%= f.password_field :password_confirmation, autocomplete: "off", :class => 'form-control'%>
</div>
<%=f.error_span(:password_confirmation) %>
</div>
<div class="form-group">
<div class="col-lg-offset-2 col-lg-3">
<%= f.label :situation %>
<%= f.check_box :situation, :class => 'col-lg-offset-3 col-lg-2' %>
</div>
<%=f.error_span(:situation) %>
</div>
<div class="form-group">
<%= f.label :function, :class => 'control-label col-lg-2' %>
<div class="col-lg-4">
<%= f.text_field :function, :class => 'form-control' %>
</div>
<%=f.error_span(:function) %>
</div>
<div class="form-group">
<%= f.label :start_Managment, :class => 'control-label col-lg-2' %>
<div class="col-lg-4">
<%= f.date_select :start_Managment, start_year: 2001, :class => 'form-control' %>
</div>
<%=f.error_span(:start_Managment) %>
</div>
<div class="form-group">
<%= f.label :dt_Admission, :class => 'control-label col-lg-2' %>
<div class="col-lg-4">
<%= f.date_select :dt_Admission, start_year: 2001, :class => 'form-control' %>
</div>
<%=f.error_span(:dt_Admission) %>
</div>
<div class="form-group">
<div class="col-lg-offset-2 col-lg-10">
<%= f.submit nil, :class => 'btn btn-primary' %>
<%= link_to t('.cancel', :default => t("helpers.links.cancel")),
members_path, :class => 'btn btn-default' %>
</div>
</div>
<% end %>
members_controller
class MembersController < InheritedResources::Base
before_action :set_member, only: [:show, :edit, :update, :destroy]
before_action :authenticate_member!
# GET /members
# GET /members.json
def pundit_user
current_member
end
def pundit_user
current_member
end
def index
@members = Member.all
end
# GET /members/1
# GET /members/1.json
def show
end
# GET /members/new
def new
@members = Member.all
authorize @members
new!
end
# GET /members/1/edit
def edit
@members = Member.all
authorize @members
end
# POST /members
# POST /members.json
def create
@members = Member.all
authorize Member
create! { members_url }
end
# PATCH/PUT /members/1
# PATCH/PUT /members/1.json
def update
update! {members_url }
end
# DELETE /members/1
# DELETE /members/1.json
def destroy
@members = Member.all
authorize @members
destroy! { members_url }
end
private
# Use callbacks to share common setup or constraints between actions.
def set_member
@member = Member.find(params[:id])
end
def set_permit
@members = Member.all
end
private
def permitted_params
params.permit(:member => [:name, :email, :password, :password_confirmation,:dt_Admission, :situation, :function, :start_Managment])
end
# Never trust parameters from the scary internet, only allow the white list through.
def member_params
params.require(:member).permit(:name, :email, :password, :password_confirmation, :dt_Admission, :situation, :function, :start_Managment)
end
end
'rake routes'的结果
Routes
Routes match in priority from top to bottom
Helper HTTP Verb Path Controller#Action
Path / Url
registration_path POST /members(.:format) registrations#create
members_admin_index_path GET /members_admin(.:format) members#index
POST /members_admin(.:format) members#create
new_members_admin_path GET /members_admin/new(.:format) members#new
edit_members_admin_path GET /members_admin/:id/edit(.:format) members#edit
members_admin_path GET /members_admin/:id(.:format) members#show
PATCH /members_admin/:id(.:format) members#update
PUT /members_admin/:id(.:format) members#update
DELETE /members_admin/:id(.:format) members#destroy
new_member_session_path GET /members/sign_in(.:format) devise/sessions#new
member_session_path POST /members/sign_in(.:format) devise/sessions#create
destroy_member_session_path DELETE /members/sign_out(.:format) devise/sessions#destroy
member_password_path POST /members/password(.:format) devise/passwords#create
new_member_password_path GET /members/password/new(.:format) devise/passwords#new
edit_member_password_path GET /members/password/edit(.:format) devise/passwords#edit
PATCH /members/password(.:format) devise/passwords#update
PUT /members/password(.:format) devise/passwords#update
cancel_member_registration_path GET /members/cancel(.:format) registrations#cancel
member_registration_path POST /members(.:format) registrations#create
new_member_registration_path GET /members/sign_up(.:format) registrations#new
edit_member_registration_path GET /members/edit(.:format) registrations#edit
PATCH /members(.:format) registrations#update
PUT /members(.:format) registrations#update
DELETE /members(.:format) registrations#destroy
GET /members_admin/:nome(.:format) members#edit
member_admin_index_path GET /member_admin(.:format) member_admin#index
POST /member_admin(.:format) member_admin#create
new_member_admin_path GET /member_admin/new(.:format) member_admin#new
edit_member_admin_path GET /member_admin/:id/edit(.:format) member_admin#edit
member_admin_path GET /member_admin/:id(.:format) member_admin#show
PATCH /member_admin/:id(.:format) member_admin#update
PUT /member_admin/:id(.:format) member_admin#update
DELETE /member_admin/:id(.:format) member_admin#destroy
managments_path GET /managments(.:format) managments#index
POST /managments(.:format) managments#create
new_managment_path GET /managments/new(.:format) managments#new
edit_managment_path GET /managments/:id/edit(.:format) managments#edit
managment_path GET /managments/:id(.:format) managments#show
PATCH /managments/:id(.:format) managments#update
PUT /managments/:id(.:format) managments#update
DELETE /managments/:id(.:format) managments#destroy
expenses_path GET /expenses(.:format) expenses#index
POST /expenses(.:format) expenses#create
new_expense_path GET /expenses/new(.:format) expenses#new
edit_expense_path GET /expenses/:id/edit(.:format) expenses#edit
expense_path GET /expenses/:id(.:format) expenses#show
PATCH /expenses/:id(.:format) expenses#update
PUT /expenses/:id(.:format) expenses#update
DELETE /expenses/:id(.:format) expenses#destroy
incomings_path GET /incomings(.:format) incomings#index
POST /incomings(.:format) incomings#create
new_incoming_path GET /incomings/new(.:format) incomings#new
edit_incoming_path GET /incomings/:id/edit(.:format) incomings#edit
incoming_path GET /incomings/:id(.:format) incomings#show
PATCH /incomings/:id(.:format) incomings#update
PUT /incomings/:id(.:format) incomings#update
DELETE /incomings/:id(.:format) incomings#destroy
members_path GET /members(.:format) members#index
POST /members(.:format) members#create
new_member_path GET /members/new(.:format) members#new
edit_member_path GET /members/:id/edit(.:format) members#edit
member_path GET /members/:id(.:format) members#show
PATCH /members/:id(.:format) members#update
PUT /members/:id(.:format) members#update
DELETE /members/:id(.:format) members#destroy
registrations_path GET /registrations(.:format) registrations#index
POST /registrations(.:format) registrations#create
new_registration_path GET /registrations/new(.:format) registrations#new
edit_registration_path GET /registrations/:id/edit(.:format) registrations#edit
GET /registrations/:id(.:format) registrations#show
PATCH /registrations/:id(.:format) registrations#update
PUT /registrations/:id(.:format) registrations#update
DELETE /registrations/:id(.:format) registrations#destroy
home_index_path GET /home/index(.:format) home#index
root_path GET / home#index
拜托,我需要帮助。
答案 0 :(得分:0)
问题在于竞争路线,看起来你几乎可以使用它。在您无法创建用户之前,或者在您登录后为您的案例成员时,我遇到过此问题。
在路径文件中尝试执行类似这样的操作,只需将用户更改为成员,并确保告诉注册控制器让创建成员默认设置为创建。
的routes.rb
devise_for :users, :controllers => { :registrations => 'registration'}, :path_names => { :'sign_up.html.erb' => 'register'}
resources :users, :only => [:index, :show, :edit, :update, :new, :create ]
resources :users_admin, :controller => 'users'
registration_controller.rb
class RegistrationController < Devise::RegistrationsController
def create
super
end
end
下次请发布一个堆栈跟踪来帮助您阅读正在发生的事情。
答案 1 :(得分:0)
你必须使用方法 PUT 或 PATCH 来编辑。路由是相同的,但HTTP方法是不同的。我认为你必须组织你的路线,因为有点困惑。
这样你就破坏了Rails模式,所以你会遇到问题。您的表单不适用于编辑和创建,因为您声明将通过POST方法响应,并且您的网址发送到 members_admin_index_path
我的意见是你应该尝试重构这个,否则你以后会遇到一些问题。