您好我是ROR的新手,我遇到路由问题。我的路线就是这样
http://localhost:3000/keys/pass.9
我想和http://localhost:3000/keys/9/pass
查看我的路线:
resources :keys , only: [:new, :show, :create, :edit, :update, :index] do
collection do
delete 'destroy_multiple'
get 'pass'
end
端
控制器:
class KeysController < ApplicationController
def pass
Key.find(params[:id]).update_attribute(:passwrod,SecureRandom.urlsafe_base64 )
respond_to do |format|
format.html { redirect_to books_path }
format.json { head :no_content }
flash[:success] = "Profile updated"
end
end
和视图:
<div class="center hero-unit">
<h1>Listing keys</h1>
<%= form_tag destroy_multiple_keys_path, method: :delete do %>
<table>
<thead>
<tr>
<th></th>
<th>Url</th>
<th>Username</th>
<th>Passwrod</th>
<th>Category</th>
</tr>
</thead>
<tbody>
<div>
<% for key in @keys %>
<% if key.book.name == @book %>
<tr>
<td><%= check_box_tag "key_ids[]", key.id %></td>
<td><%=key.url %></td>
<td><%=key.username %></td>
<td><%=key.passwrod %></td>
<td><%=key.category %></td>
<td><%= link_to 'Edit',edit_key_path(key) %></td>
<td> <%= link_to 'Change password', pass_keys_path(key) %> </td>
</tr>
<% end %>
<% end %>
<tr>
<td> <input type="button" value="check all" onclick="$(this.form).getInputs('checkbox').each(function (elem) {elem.checked = true;});" /> </td>
</tr>
</div>
<%= submit_tag "Delete selected" %>
<% end %>
<%= link_to 'New Key', new_key_path %>
<%= link_to 'Back', books_path %>
</div>
我现在得到了这个错误:
找不到没有ID的密钥
答案 0 :(得分:1)
将路线更改为
resources :keys , only: [:new, :show, :create, :edit, :update, :index] do
collection do
delete 'destroy_multiple'
end
member do
get 'pass'
end
end