【问题标题】:Ruby on Rails routesRuby on Rails 路线
【发布时间】:2014-04-23 11:47:28
【问题描述】:

您好,我是 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 的密钥

【问题讨论】:

  • member do get 'pass' end 为此,您需要成员路由,而不是集合
  • @user3438570 在路由中使用成员 ..

标签: ruby-on-rails ruby routing rails-routing


【解决方案1】:

将路线更改为

resources :keys , only: [:new, :show, :create, :edit, :update, :index]  do
  collection do
    delete 'destroy_multiple'
  end
  member do
    get  'pass'
  end
end

【讨论】:

  • 谢谢!!那行得通。我会在几分钟内把它作为解决方案。
猜你喜欢
  • 2011-02-15
  • 2011-10-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多