0

I am new to rails and I have a task asking me to let a page admin to send invitations for regular users to be admins with him I implemented the part to reply to the invitations but I am stuck in an error and please tell me if I am on the right way here is my code for the invitation method

def invite
  inviteUser = {"user_id" => current_user.id,"magazine_id" => params[:id]}
  CollaborationInvitation.create(inviteUser)
  @magazine = Magazine.find(params[:id])
  redirect_to :back
end

Here is my model for the invitation:

class CollaborationInvitation < ActiveRecord::Base
  belongs_to :user
  belongs_to :magazine
end

Model for page:

class Magazine < ActiveRecord::Base
  mount_uploader :image, ImgUploader
  has_many :articles
  acts_as_followable
  has_and_belongs_to_many :users
  errors[:image] << 'should be less than 5MB' if image.size > 5.megabytes

  validates :name, presence: true
  validates :image, presence: true
  validate :image_size
end`

And the routes:

routes`member do
  put 'follow' => 'magazines#follow'
  put 'unfollow' => 'magazines#unfollow'
  put 'invite' => 'magazines#invite'
end

error : No route matches [GET] "/magazines/3/invite" and when I changed the route from put to get

I get this error: unknown attribute 'user_id' for CollaborationInvitation.

infused
  • 24,000
  • 13
  • 68
  • 78
LSDrako
  • 63
  • 1
  • 8

1 Answers1

0

It looks like you just need to setup the routes properly as resources. Remove the routes listed above and define these instead:

resources :magazines do
  member do
    put :follow
    put :unfollow
    put :invite
  end
end
infused
  • 24,000
  • 13
  • 68
  • 78
  • I got the same error which is No route matches [GET] "/magazines/3/invite" – LSDrako May 01 '15 at 22:51
  • Pass `method: :put` in your `link_to` or whatever you are using. It's defaulting to get, when you've explicitly said to require put. – infused May 01 '15 at 22:53
  • I already have this in my link thats why I have no idea where is the problem – LSDrako May 01 '15 at 23:00
  • `
    <%if can? :invite, @magazine%> <%=link_to'Invite'.html_safe ,controller: :magazines,action: :invite, method: :put %> <%end%>`
    – LSDrako May 01 '15 at 23:04
  • Wrap `method: :put` in braces: `{method: :put}`, because its getting passed to the url_options and it should be passed to html_options. – infused May 01 '15 at 23:12
  • actually it raised a new error which is unknown attribute 'user_id' for CollaborationInvitation. ` inviteUser = {"user_id" => current_user.id,"magazine_id" => params[:id]} CollaborationInvitation.create(inviteUser) @magazine =Magazine.find( params[:id]) redirect_to :back end ` which is in the magazine controller – LSDrako May 01 '15 at 23:20
  • Does the CollaborationInvitation model have a user_id column in the database? – infused May 01 '15 at 23:23