简体   繁体   English

没有ID找不到声音-Stuck Ruby On Rails

[英]Couldn't find Sound without an ID - Stuck Ruby On Rails

I'm beginner at Rails, and im actually stuck with apparently a "beginner problem". 我是Rails的初学者,而我实际上显然陷入了“初学者问题”。

I got a "sounds" scaffolded controller, and i had to add an action "sendit". 我有一个“声音”支架式控制器,我必须添加一个动作“ sendit”。 I can't have access to the sound from my view to my controller. 我无法从我的视图访问控制器的声音。

This is my error when i try to access to " http://127.0.0.1:3000/sounds/sendit.14 " (Why it's sendit.14 and not sounds/sendit/14 or sounds/14/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"}

Here is my code : 这是我的代码:

Sound Controller : 声音控制器:

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


Index.html.erb Index.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>


routes.rb 的routes.rb

  devise_for :users

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

  resources :users, :sounds

I did this in the route file because of Adding an action to an existing controller (Ruby on Rails) 我在路由文件中执行此操作是因为将操作添加到现有控制器(Ruby on Rails)


When i do rake routes , this is the output : 当我做耙路线时 ,这是输出:

              [...]
           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

(I actually don't understand the first line, why there is no POST / GET, and why it's sounds_sendit and no sendit_sound like others default actions? How to fix it? ) (我实际上不明白第一行,为什么没有POST / GET,为什么像其他默认操作一样是sounds_sendit和sendit_sound?如何解决?)

Thank you for your help 谢谢您的帮助

Because you don't have route to the action with id as param, so rails assumes that id is format You have to create route for that 因为您没有将id用作参数的操作的路由,所以rails假定id为format您必须为此创建路由

resources :sounds do
  post :sendit, on: :member
end

Instead of this : 代替这个:

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

Use this: 用这个:

  resources :sounds do
    member do
      get :sendit
    end
  end

Then you will be able to use the sendit_sound_path helper to link to the action correctly: 然后,您将能够使用sendit_sound_path帮助器正确链接到操作:

link_to "link text", sendit_sound_path(sound_object)

Hope this does it for you,... 希望这对您有帮助...

You should define in your routes the id parameter, see here . 您应该在路由中定义id参数,请参见此处

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

You can name your route by using the 'as' option. 您可以使用“ as”选项来命名路线。 An example from the above site: 来自上述站点的示例:

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

If you only want a get, then you can use get instead of match. 如果只想获取,则可以使用get代替match。 An example: 一个例子:

either: 之一:

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

or shorter: 或更短:

get 'photos/show'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM