简体   繁体   中英

How to pass a parameter to another View in Ruby on Rails

I have a Model called Year, and a Model called Highlight

A Year has and belongs to many highlights. And a highlight, has and belongs to many years. As this:

class Year < ActiveRecord::Base

  has_and_belongs_to_many :highlights

end

class Highlight < ActiveRecord::Base
    has_and_belongs_to_many :years
end

I have a view on the Year entity and want to have a button that links to all highlights of that specific year, for example:

 <td>
 <%= button_to 'Highlights', admin_highlights_path(year), class: 'btn btn-sm btn-default' %>
</td>

I know what to do on my controller, but Im doing something wrong on this view. Because, when I click the button it show the message:

ActionController::ParameterMissing in Admin::HighlightsController#create

Because, of course this is trying to go to the wrong action.

So, my question is, how to generate a button that send the year as parameter for the admin_highlight index, and in my controller I can show all the highlights categories for that specific year?

My routes:

namespace :admin do
    root 'pages#show', id: 'dashboard'

    resources :events
    resources :invitees do
      collection do
        get 'import_invitees/:year' => 'invitees#import_invitees', as: :import_invitees
      end
    end
    resources :highlights

    resources :pages
    resources :speakers
    resources :sponsors
    resources :users
    resources :years do
      resources :events do
        resources :photos
      end
    end
  end

While defining the path to the button you should pass the year as a param. Thats look like as follow: routes_path(key: value) ex: admin_highlights_path(year: year)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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