0

我是 Rails 的初学者,我实际上显然是被一个“初学者问题”困住了。

我有一个“声音”脚手架控制器,我必须添加一个动作“sendit”。我无法访问从我的视图到我的控制器的声音。

这是我尝试访问http://127.0.0.1:3000/sounds/sendit.14 ”时的错误(为什么它是 sendit.14 而不是sounds/sendit/14 或sounds/14/sendit?)

ActiveRecord::RecordNotFound in SoundsController#sendit

Couldn't find Sound without an ID

Application Trace | Framework Trace | Full Trace
app/controllers/sounds_controller.rb:74:in `sendit'
Request

Parameters:

{"format"=>"14"}

这是我的代码:

声音控制器:

def sendit
    @sound = Sound.find(params[:id]) # ----- Error is on this line -----
    # Do Job
  end


索引.html.erb

<% @sounds.each do |sound| %>
  <% if sound.user_id == current_user.id %>
    <tr>
      <td><%= sound.title %></td>
      <td><%= sound.user.email %></td>
      <td><%= link_to 'Show', sound %></td>
      <td><%= link_to 'Edit', edit_sound_path(sound) %></td>
      <td><%= link_to 'Destroy', sound, method: :delete, data: { confirm: 'Are you sure?' } %></td>
      <td><%= link_to 'Send', sounds_sendit_path(sound) %></td>


路线.rb

  devise_for :users

  match "/sounds/sendit/", :controller => "sounds", :action => "sendit"

  resources :users, :sounds

我在路由文件中这样做是因为向现有控制器添加操作(Ruby on Rails)


当我做rake routes时,这是输出:

              [...]
           sounds_sendit        /sounds/sendit(.:format)       sounds#sendit 
                   users GET    /users(.:format)               users#index
                         POST   /users(.:format)               users#create
                new_user GET    /users/new(.:format)           users#new
               edit_user GET    /users/:id/edit(.:format)      users#edit
                    user GET    /users/:id(.:format)           users#show
                         PUT    /users/:id(.:format)           users#update
                         DELETE /users/:id(.:format)           users#destroy
                  sounds GET    /sounds(.:format)              sounds#index
                         POST   /sounds(.:format)              sounds#create
               new_sound GET    /sounds/new(.:format)          sounds#new
              edit_sound GET    /sounds/:id/edit(.:format)     sounds#edit
                   sound GET    /sounds/:id(.:format)          sounds#show
                         PUT    /sounds/:id(.:format)          sounds#update
                         DELETE /sounds/:id(.:format)          sounds#destroy
                    root        /                              home#index

(我其实不明白第一行,为什么没有POST / GET,为什么是sounds_sendit 和没有sendit_sound 像其他默认操作?如何解决?)

感谢您的帮助

4

3 回答 3

2

因为您没有以 id 作为参数的操作的路线,所以 rails 假定 id 是格式您必须为此创建路线

resources :sounds do
  post :sendit, on: :member
end
于 2012-07-31T09:14:06.577 回答
2

而不是这个:

match "/sounds/sendit/", :controller => "sounds", :action => "sendit"

用这个:

  resources :sounds do
    member do
      get :sendit
    end
  end

然后您将能够使用sendit_sound_path帮助程序正确链接到操作:

link_to "link text", sendit_sound_path(sound_object)

希望这对你有用,...

于 2012-07-31T09:16:31.600 回答
1

您应该在您的路线中定义 id 参数,请参见此处

match "/sounds/:id/sendit/", :controller => "sounds", :action => "sendit"

您可以使用“as”选项命名您的路线。来自上述网站的示例:

match 'exit' => 'sessions#destroy', :as => :logout

如果你只想要一个 get,那么你可以使用 get 而不是 match。一个例子:

任何一个:

match 'photos/show' => 'photos#show', :via => :get

或更短:

get 'photos/show'
于 2012-07-31T09:14:18.437 回答