samedi 27 juin 2015

has_many :through broke some code

So i'm relatively new to RoR, and am having some issues in trying to get my code back up and working. So previously I had users, and wikis that users could create. I've set up so that users can subscribe and get premium status to make wikis private. Now I'm in the process of making it so that Premium users can add standard users as collaborators to the wiki. I've decided to got about associating them through has_many :through relationships.

The issue I'm running into so that some of my buttons have started making errors that I don't understand. The one I'm stuck on right now is when showing the page that has a create new wiki button on it.

This is the error I am getting when I added the has_many through: relationship

No route matches {:action=>"new", :controller=>"wikis", :format=>nil, :user_id=>nil} missing required keys: [:user_id]

Here are the models:

collaborator.rb

class Collaborator < ActiveRecord::Base
  belongs_to :wiki
  belongs_to :user
end

user.rb

class User < ActiveRecord::Base

  ...

  has_many :collaborators
  has_many :wikis, :through => :collaborators

end

wiki.rb

class Wiki < ActiveRecord::Base

  belongs_to :user

  has_many :collaborators
  has_many :users, :through => :collaborators

end

The important bits of the wiki_controller.rb

def new
  @user = User.find(params[:user_id])
  @wiki = Wiki.new
  authorize @wiki
end

def create
  @user = current_user
  @wiki = @user.wikis.create(wiki_params)
  authorize @wiki
  if @wiki.save
    flash[:notice] = "Wiki was saved"
    redirect_to @wiki
  else
    flash[:error] = "There was an error saving the Wiki. Please try again"
    render :new
  end
end

And finally the show.html.erb file the button is located in.

<div class="center-align">
  <%= link_to "New Wiki", new_user_wiki_path(@user, @wiki), class: 'btn grey darken-1' %>
</div>

If I'm missing any files or relevant info please let me know. This may be a simple stupid answer but I'm stuck for the life of me.

Thanks in advance.

Edit:

Here is the requested added info, first up the show info in the users_controllers.rb

def show
  @wikis = policy_scope(Wiki)
end

the corresponding policy scope I'm using in the user_policy.rb

class UserPolicy < ApplicationPolicy

  class Scope
    attr_reader :user, :scope

    def initialize(user, scope)
      @user = user
      @scope = scope
    end

    def resolve
      wikis = []
      all_wikis = scope.all
      all_wikis.each do |wiki|
        if wiki.user == user || wiki.users.include?(user)
          wikis << wiki
        end
      end
    end
    wikis
  end

end

and the route.rb file

Rails.application.routes.draw do

  devise_for :users
  resources :users, only: [:update, :show] do
    resources :wikis, shallow: true
  end

  resources :wikis, only: [:index]
  resources :charges, only: [:new, :create]
  delete '/downgrade', to: 'charges#downgrade'

  authenticated do
    root to: "users#show", as: :authenticated
  end

  root to: 'welcome#index'

end

Hope it helps

Aucun commentaire:

Enregistrer un commentaire