我是Rails中的新手,我遇到了路线问题。当我尝试删除一个控件时,会出现一条错误,上面写着“No route matches [DELETE]”/months.5“。我不知道为什么。我告诉你我的代码。
Ruotes.rb
Rails.application.routes.draw do
root 'staticpages#index'
devise_for :users # :path => '',
# path_names: {sign_in: 'login', sign_up: 'registro'},
# controllers: {sessiones: 'users/controls'}
get '/controls/calendar' => 'controls#event'
get 'graphicscontrols' => 'graphics#graphics_evolution'
get 'months' => 'controls#months'
# resources :users, only: [:show] do #:path => ''
resources :controls # :path => ''
resources :graphics, only: [:index]
resources :meals, only: [:index, :new, :create]
控制控制器
类ControlsController< ApplicationController中
before_action :authenticate_user!
def index
@user= current_user
@control= Control.new
@control_last= @user.controls.last
@controls= @user.controls.order_by_date
@controls_average = Control.controls_average (current_user.id)
@controls_average_day = Control.controls_day_average (current_user.id)
# @controls = Control.order_by_date
end
def event
@user= current_user
@controls=@user.controls.all
render json: @controls if request.xhr?
end
def months
@user= current_user
@controls= @user.controls.order_by_date
end
def create
@user = current_user
control_params2 ={}
control_params2[:level] = control_params[:level]
control_params2[:period] = control_params[:period]
control_params2[:day]=DateTime.strptime(control_params[:day],'%m/%d/%Y %I:%M %p')
@control= @user.controls.build(control_params2)
@control.save!
flash[:notice] = "Congratulations, your control has been created"
redirect_to controls_path
rescue ActiveRecord::RecordInvalid
render 'index'
end
def edit
@control = Control.find(params[:id])
end
def update
@user = current_user
@control = Control.find(params[:id])
if @control.update_attributes(control_params)
flash[:notice] = "Congratulations, your control has been updated"
redirect_to controls_path(current_user)
else
@errors = @control.errors.full_messages
render 'edit'
end
end
def delete
@control= Control.find(params[:id])
end
def destroy
@control= Control.find(params[:id])
if @control.delete
flash[:notice] = "Congratulations, your control has been removed"
redirect_to months_path
end
end
private
def control_params
params.require(:control).permit(:level, :period, :day)
end
端
视图/控制/ moonths.html.erb
<h1>Controls</h1>
<ul>
<% @controls.each do |control| %>
<li><%= "Blood Sugar Control:" + control.level.to_s + " - " + control.period + " - " + control.day.to_s %></li>
<%= link_to "Delete", months_path(control), method:'delete', data: {confirm: "Are you sure?"} %>
<%= link_to "edit", edit_control_path(control)%>
</ul>
<% end %>
答案 0 :(得分:0)
您可以查看rake routes
一次,了解更多路线
现在你可以做的是
<%= link_to "Delete", control, method:'delete', data: {confirm: "Are you sure?"} %>
或
<%= link_to "Delete", control_path(control.id), method:'delete', data: {confirm: "Are you sure?"} %>
您正在删除Control对象使用control_path(..),或者对于Month对象使用month_path(..)
答案 1 :(得分:-1)
在你看来你应该把这个
<h1>Controls</h1>
<ul>
<% @controls.each do |control| %>
<li><%= "Blood Sugar Control:" + control.level.to_s + " - " + control.period + " - " + control.day.to_s %></li>
<%= link_to "Delete", controls_path(control), method:'delete', data: {confirm: "Are you sure?"} %>
<%= link_to "edit", edit_control_path(control)%>
</ul>
<% end %>