没有ID错误就找不到实习

时间:2019-11-04 23:15:31

标签: ruby-on-rails ruby rubygems

嗨,所以我真的是Rails的新手,我正在尝试弄清楚如何使用这个https://github.com/jonhue/acts_as_favoritor宝物。

此刻,我正试图让学生最喜欢实习。

我的收藏夹控制器如下所示

    class FavouritesController < ApplicationController

    def new
        @internship = Internship.find(params[:id])
        current_student.favourite(internship)
        redirect_to_show
    end

    def show
        current_students.all_favourites
    end

end

我的路线如下

Rails.application.routes.draw do
  resources :favourites
  devise_for :students
  resources :internships
  devise_for :companies
  # For details on the DSL ava

添加索引中显示的实习机会的按钮是这个

<td><%= link_to 'favourite', new_favourite_path %></td>

使用我在学生模型中放入act_as_favoritor模型和在实习模型中放入act_as_favoritable模型的宝石。多年来,我一直在脑海中摸索,试图了解如何编写方法,以便有人可以帮助我,谢谢您!

1 个答案:

答案 0 :(得分:1)

我想如果我是你,我的路线会像这样:

resources :internships do 
  member do 
    post :favorite
    post :unfavorite
  end
end

除其他外,还会给您:

  favorite_internship POST   /internships/:id/favorite(.:format)     internships#favorite
unfavorite_internship POST   /internships/:id/unfavorite(.:format)   internships#unfavorite
          internships GET    /internships(.:format)                  internships#index
                      POST   /internships(.:format)                  internships#create
       new_internship GET    /internships/new(.:format)              internships#new
      edit_internship GET    /internships/:id/edit(.:format)         internships#edit
           internship GET    /internships/:id(.:format)              internships#show
                      PATCH  /internships/:id(.:format)              internships#update
                      PUT    /internships/:id(.:format)              internships#update
                      DELETE /internships/:id(.:format)              internships#destroy

然后在您看来,您将按照以下方式进行操作:

<td>
  <% if current_student.favorited?(internship) %>
    <%= link_to 'unfavourite', unfavorite_internship_path(internship), method: :post %>
  <% else %>
    <%= link_to 'favourite', favorite_internship_path(internship), method: :post %>
  <% end %>
</td>

自然,这假定您可以访问视图中的current_studentinternship

然后,在您的InternshipsController中,您将执行以下操作:

class InternshipsController < ApplicationController

  def favorite
    @internship = Internship.find(params[:id])
    current_student.favorite(@internship)
    # redirect somewhere
  end

  def unfavorite
    @internship = Internship.find(params[:id])
    current_students.unfavorite(@internship)
    # redirect somewhere
  end

end

现在,favoriteunfavorite并不是很安静。所以,我想你可以做到:

resources :internships do 
  scope module: :internships do 
    resources :favorites, only: [:create] do 
      collection do 
        delete '/', action: :destroy
      end
    end
  end
end

哪个会给你:

internship_favorites DELETE /internships/:internship_id/favorites(.:format)   internships/favorites#destroy
                     POST   /internships/:internship_id/favorites(.:format)   internships/favorites#create
         internships GET    /internships(.:format)                            internships#index
                     POST   /internships(.:format)                            internships#create
      new_internship GET    /internships/new(.:format)                        internships#new
     edit_internship GET    /internships/:id/edit(.:format)                   internships#edit
          internship GET    /internships/:id(.:format)                        internships#show
                     PATCH  /internships/:id(.:format)                        internships#update
                     PUT    /internships/:id(.:format)                        internships#update
                     DELETE /internships/:id(.:format)                        internships#destroy

那么您将需要Internships::FavoritesController之类的东西

# in app/controllers/internships/favorites_controller.rb
class Internships::FavoritesController < ApplicationController

  def create
    @internship = Internship.find(params[:internship_id])
    current_student.favorite(@internship)
    # redirect somewhere
  end

  def destroy
    @internship = Internship.find(params[:internship_id])
    current_students.unfavorite(@internship)
    # redirect somewhere
  end

end

然后在您看来,它更像是:

<td>
  <% if current_student.favorited?(internship) %>
    <%= link_to 'unfavourite', internship_favorites_path(internship), method: :delete %>
  <% else %>
    <%= link_to 'favourite', internship_favorites_path(internship), method: :post %>
  <% end %>
</td>