问题
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