我正在开发我的第一个Rails项目,而且我遇到了一个持续存在的问题。我怀疑它与路由有关,但是,我似乎无法在网上找到任何关于它的信息。
我认为这是一个相当简单的修复,所以请看看,如果你能提供帮助,请告诉我。
TL; DR
我想要实现的目标
发生了什么:
Image of error, Showing Rails getting a different ID
控制器
class AccountdetailsController < ApplicationController
def index
@accountdetail = Accountdetail.all
end
#I can't find the ID to show the relevent card.
def show
@accountdetail = Accountdetail.find(params[:id])
if @accountdetail.nil?
redirect_to accountdetail_path
end
end
def new
@accountdetail = Accountdetail.new
end
def edit
@accountdetail = Accountdetail.find(params[:id])
end
def create
@accountdetail = Accountdetail.new(accountdetail_params)
if @accountdetail.save
redirect_to @accountdetail
else
render 'new'
end
end
#it affects this
def update
@accountdetail = Accountdetail.find(params[:id])
if @accountdetail.update(accountdetail_params)
redirect_to accountdetail
else
render 'edit'
end
end
#and this
def destroy
@accountdetail = Accountdetail.find(params[:id])
@accountdetail.destroy
redirect_to accountdetail_path
end
private def accountdetail_params
params.require(:accountdetail).permit(:first_name, :last_name, :phone, :notes, :id)
end
end
Index.HTML.ERB
<div class="ui card">
<div class="content">
<a class="header"><%= account.first_name %> <%= account.last_name %> </a>
<div class="meta">
<span class="date"><%= account.phone %></span>
<strong><p><%= account.notes %></p></strong> <br>
<%= link_to "edit", edit_accountdetail_path(@accountdetail) %>
<%= link_to 'Inspect', accountdetail_path(@accountdetail) %>
</div>
</div>
</div>
<% end %>
路线
Rails.application.routes.draw do
get 'welcome/index'
resources :articles do
resources :comments
end
resources :accountdetails
root 'welcome#index'
end
答案 0 :(得分:0)
在index.html.erb中替换以下
<%= link_to "edit", edit_accountdetail_path(@accountdetail) %>
<%= link_to 'Inspect', accountdetail_path(@accountdetail) %>
与
<%= link_to "edit", edit_accountdetail_path(account) %>
<%= link_to 'Inspect', accountdetail_path(account) %>
@accountdetail
为您提供了帐户的所有记录,因为它在控制器中触发了选择查询。但在这里我们只需要一个实例,account
。
希望这有帮助。