Ruby on rails 面对“U到”链接的问题;编辑";在轨
我正在尝试以带有编辑选项的表格格式显示所有指标。但是,我最终得到了以下错误 在索引视图中,我可以看到所有数据。但当我点击编辑链接时,它并没有重定向到编辑视图,在那个里我有不同的列要显示 索引视图:Ruby on rails 面对“U到”链接的问题;编辑";在轨,ruby-on-rails,ruby,ruby-on-rails-4,Ruby On Rails,Ruby,Ruby On Rails 4,我正在尝试以带有编辑选项的表格格式显示所有指标。但是,我最终得到了以下错误 在索引视图中,我可以看到所有数据。但当我点击编辑链接时,它并没有重定向到编辑视图,在那个里我有不同的列要显示 索引视图: <%= form_for :metrics_controller, url: metrics_path(@metric), method: :get do |f| %> <table id="metrics"> <thead> <tr i
<%= form_for :metrics_controller, url: metrics_path(@metric), method: :get do |f| %>
<table id="metrics">
<thead>
<tr id="AllMetricColumnNames">
<th id="CommentsColumn">Comments</th>
<th id="EditColumn">Edit</th>
</tr>
</thead>
<% @metricAll.each do |data| %>
<tr id="AllMetricValues">
<td id="Comments"><%= data.Comments %></td>
<td id="EditButton"><%= link_to "Edit", edit_metric_path(@metricAll) %></td>
<% end %>
</tr>
</table>
<% end %>
class MetricsController < ApplicationController
def index
@metricAll = Metric.all
end
def show
@metric = Metric.find(params[:id])
end
def edit
@metric = Metric.find(params[:id])
end
private def post_params
params.require(:metric).permit(:Metric, :Comments)
end
end
root 'metrics#index'
get 'index' => 'metrics#index'
get 'edit' => 'metrics#edit'
resources :metrics
您正在传递编辑路由的所有指标。离开
<td id="EditButton"><%= link_to "Edit", edit_metric_path(@metricAll) %></td>
到
数据
是代码中的当前度量根据屏幕截图,错误在模型中。
另外,正如其他人所提到的,您应该删除那些get
路由,因为resources:metrics
将为您的所有CRUD操作a.ka生成必要的路由。对于索引,显示、编辑、新建、创建、更新、销毁
我的猜测是,metric.rb
文件有一个属于:automated\u thresholding
关系,但是metrics
数据库表缺少automated\u thresholding\u id
字段
您应该创建一个迁移来添加该字段
add_reference:metrics,:automated_thresholding
现在,我得到了这个错误:ActionController::UrlGenerationError in metrics#index No route matches{:action=>“edit”,:controller=>“metrics”,:id=>nil}缺少必需的键:[:id]您的数据变量为零-尝试将其替换为要编辑的对象的ID删除get
路由,然后重试。是的,删除此行get'edit'=>“metrics”#edit'
。这已经在resources:metrics
betterI中定义了,我肯定认为这就是问题所在。如果您的主键与id不同,请尝试此self.primary\u key=''
为什么要添加get
路由?我认为resources:metrics
如果您想使用默认的rails路由模式就足够了。这是我在模型中拥有的:class Metric自动阈值的数据库表。如果是这种情况,则此语法self.table\u name=AutomatedThresholding
是错误的。这应该是正确的self.table\u name='automated\u thresholdings'
阅读更多信息:
<td id="EditButton"><%= link_to "Edit", edit_metric_path(data) %></td>