gpt4 book ai didi

ruby-on-rails - Rails 缺少必需的键 : [:id]

转载 作者:太空宇宙 更新时间:2023-11-03 17:47:24 24 4
gpt4 key购买 nike

我一直在尝试为用户建立一个基本的网站博客。我有将“联系”页面制作成数据库的想法,您可以在其中操纵进入内部的链接。并编辑它们等。但都在同一页面上避免创建 edit.html.erbshow.html.erb .只是索引和一个 _form ,我被踩住了。顺便说一句,这是我尝试用任何语言开发的第一个网站。

这是 index.html.erb这让我一直失败。

<div class="indent50">
<h3>For any questions contact me</h3>
<% @contacts.each do |contact| %>
<h5><%= link_to contact.name, "http://#{contact.clink}" %></h5>
<h5><%= link_to "Edit",edit_contact_path(@contact) %></h5>
<% end %>
</div>
<%= render "form" %>

提高No route matches {:action=>"edit", :controller=>"contacts", :id=>nil} missing required keys: [:id]这里<h5><%= link_to "Edit",edit_contact_path(@contact) %></h5>

这是联系人 Controller :

class ContactsController < ApplicationController
before_action :find_contact, only: [:show, :edit, :update, :destroy]
def index
@contacts = Contact.all.order("created_at DESC")
end
def new
@contact = Contact.new
end

def create
@contact = Contact.new(post_params)
if @contact.save
flash[:notice] = "Contact created"
redirect_to(:action=>'index', :contact_id => @contact.id)
else
@contacts = Contacts.order()
render('new')
end
end

def edit
end

private
def find_contact
@contact=Contact.find(params[:id])
end
def post_params
params.require(:contact).permit(:name, :clink)
end
end

有什么建议吗?甚至替代我的想法。谢谢。

最佳答案

您需要在 link_to "Edit" 行中将 @contact 替换为 contact

@contact 不存在,您需要使用您创建的名为contact 的循环变量。这就是为什么它说“缺少必需的 key ”,因为您发送的 @contact 的值是 nil

关于ruby-on-rails - Rails 缺少必需的键 : [:id],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33105352/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com