Getting a strange error on my routes, “missing required keys” rails 4

走远了吗. 提交于 2019-12-14 03:04:36

问题


So I am getting this error on my localhost:3000. I am making a gif posting blog app and I am trying to add an edit function to items that appear in my _feed_item.html.erb partial. I have posted the error below and a link to some source files in a gist. I believe the routes are ok, but the "missing required keys: [:id]" piece is the one aspect I don't understand. Not sure why this doesn't work as well as my delete function does. Some of this code is based on Hartl's rails tutorial.

app/views/shared/_feed_item.html.erb where line #25 raised:

No route matches {:controller=>"microposts", :action=>"edit"} missing required keys: [:id]

Extracted source (around line #25(see the bold line below)):

</span><br />

<% if current_user?(feed_item.user) %>
    **<%= link_to "edit", edit_micropost_path %>**
    <%= link_to "delete", feed_item, method: :delete, 
                data: { confirm: "Are you sure? "}, title: feed_item.content %>
<% end %>
 </li>

Link to Gist


回答1:


Your're almost there. The error message says,

No route matches {:controller=>"microposts", :action=>"edit"} missing required keys: [:id].

The controller has to know which resource (micropost) you are trying to access. Give the id of the resource and you'll be all fine.

**<%= link_to "edit", edit_micropost_path(feed_item.id) %>**



回答2:


You need to give your edit path a parameter so it knows the id of the micropost to edit.

edit_micropost_path(feed_item)


来源:https://stackoverflow.com/questions/22851964/getting-a-strange-error-on-my-routes-missing-required-keys-rails-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!