我是 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 像其他默认操作?如何解决?)
感谢您的帮助