问题
I'm having trouble with my Rails 4 app.
I'm trying to follow this tutorial https://coderwall.com/p/rqjjca/creating-a-scoped-invitation-system-for-rails with some modifications.
The tutorial skips over a few steps that are considered by the writer to be too easy to bother with - I think that's where I'm getting stuck.
I have models for profile, project, team and user.
The associations are:
Profile.rb
has_many :teams, foreign_key: "team_mate_id"
has_many :team_projects, through: :teams, source: :project
has_many :invitations, :class_name => "Invite", :foreign_key => 'recipient_id'
has_many :sent_invites, :class_name => "Invite", :foreign_key => 'sender_id'
Project.rb
belongs_to :profile
has_one :team
has_many :team_mates, through: :team
has_many :invites
Team.rb
belongs_to :project
belongs_to :team_mate, class_name: "Profile"
Invite.rb
belongs_to :project
belongs_to :sender, :class_name => 'Profile'
belongs_to :recipient, :class_name => 'Profile'
In my routes, I have:
resources :invites
resources :teams
resources :profiles, only: [:show, :edit, :update, :destroy]
resources :projects do
member do
put "invite_team_mates" => "projects/invite_team_mates", as: :invitation
end
In my projects views folder, I have a partial with the invite form:
<%= form_for @invite , :url => invites_path do |f| %>
<%= f.hidden_field :project_id, :value => @invite.project_id %>
<%= f.label :email %>
<%= f.email_field :email %>
<%= f.submit 'Send' %>
<% end %>
I have also tried this as a general file called invite_team_mates.html.erb inside the project views folder and made a matching action for it in the projects controller, which has the action below: (with the route shown above for that specific action).
def invite_team_mate
project = Project.find(params[:id])
authorize @project
end
I'm also not sure if I have to change the opening line of form_for @invite should be something to do with @project.invite. Im not sure.
I also have an invites controller with:
class InvitesController < ApplicationController
def new
@invite = Invite.new
end
def create
@invite = Invite.new(invite_params)
@invite.sender_id = current_user.profile.id
if @invite.save
#if the user already exists
if @invite.recipient != nil
#send existing user email invitation to join project team
InviteMailer.existing_user_invite(@invite).deliver
#Add the user to the user group - inivte rsvp pending
@invite.recipient.project.push(@invite.project)
else
#send new user email invitation to join as a general user as well as join this project team
InviteMailer.new_user_invite(@invite, new_user_registration_path(:invite_token => @invite.token)).deliver
end
else
# oh no, creating an new invitation failed
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_invite
@invite = Invite.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def invite_params
params[:invite].permit(:email)
end
end
Im really stuck for how to hang all of this together.
My objective is to allow a user who creates a project to invite others to join the project team. I'm not getting far with this. When I try, to save all this and open the project, I get an error that says:
First argument in form cannot contain nil or be empty
The error highlights this line of the invites form partial:
<%= form_for @invite , :url => invites_path do |f| %>
When I try to change this line to use simple form syntax, as follows:
<%= simple_form_for(@invite, :url => invites_path) do |f| %>
I get an error that says:
undefined method `model_name' for nil:NilClass
I think that might have something to do with the form being for @invite when it is saved in the projects views folder.
Can anyone see what's gone wrong?
update - terminal lines
NoMethodError - undefined method `model_name' for nil:NilClass:
actionpack (4.2.4) lib/action_controller/model_naming.rb:9:in `model_name_from_record_or_class'
actionview (4.2.4) lib/action_view/record_identifier.rb:47:in `dom_class'
simple_form (3.2.0) lib/simple_form/action_view_extensions/form_helper.rb:62:in `simple_form_css_class'
simple_form (3.2.0) lib/simple_form/action_view_extensions/form_helper.rb:22:in `simple_form_for'
app/views/projects/_invite_team_mate.html.erb:6:in `_app_views_projects__invite_team_mate_html_erb___1258064784578419157_70357207009700'
actionview (4.2.4) lib/action_view/template.rb:145:in `block in render'
activesupport (4.2.4) lib/active_support/notifications.rb:166:in `instrument'
actionview (4.2.4) lib/action_view/template.rb:333:in `instrument'
actionview (4.2.4) lib/action_view/template.rb:143:in `render'
actionview (4.2.4) lib/action_view/renderer/partial_renderer.rb:339:in `render_partial'
actionview (4.2.4) lib/action_view/renderer/partial_renderer.rb:310:in `block in render'
actionview (4.2.4) lib/action_view/renderer/abstract_renderer.rb:39:in `block in instrument'
activesupport (4.2.4) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (4.2.4) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.2.4) lib/active_support/notifications.rb:164:in `instrument'
actionview (4.2.4) lib/action_view/renderer/abstract_renderer.rb:39:in `instrument'
actionview (4.2.4) lib/action_view/renderer/partial_renderer.rb:309:in `render'
actionview (4.2.4) lib/action_view/renderer/renderer.rb:47:in `render_partial'
actionview (4.2.4) lib/action_view/renderer/renderer.rb:21:in `render'
actionview (4.2.4) lib/action_view/helpers/rendering_helper.rb:32:in `render'
haml (4.0.7) lib/haml/helpers/action_view_mods.rb:12:in `render_with_haml'
app/views/projects/show.html.erb:976:in `_app_views_projects_show_html_erb__3676206074319755518_70357179499400'
actionview (4.2.4) lib/action_view/template.rb:145:in `block in render'
activesupport (4.2.4) lib/active_support/notifications.rb:166:in `instrument'
actionview (4.2.4) lib/action_view/template.rb:333:in `instrument'
actionview (4.2.4) lib/action_view/template.rb:143:in `render'
actionview (4.2.4) lib/action_view/renderer/template_renderer.rb:54:in `block (2 levels) in render_template'
actionview (4.2.4) lib/action_view/renderer/abstract_renderer.rb:39:in `block in instrument'
activesupport (4.2.4) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (4.2.4) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.2.4) lib/active_support/notifications.rb:164:in `instrument'
actionview (4.2.4) lib/action_view/renderer/abstract_renderer.rb:39:in `instrument'
actionview (4.2.4) lib/action_view/renderer/template_renderer.rb:53:in `block in render_template'
actionview (4.2.4) lib/action_view/renderer/template_renderer.rb:61:in `render_with_layout'
actionview (4.2.4) lib/action_view/renderer/template_renderer.rb:52:in `render_template'
actionview (4.2.4) lib/action_view/renderer/template_renderer.rb:14:in `render'
actionview (4.2.4) lib/action_view/renderer/renderer.rb:42:in `render_template'
actionview (4.2.4) lib/action_view/renderer/renderer.rb:23:in `render'
actionview (4.2.4) lib/action_view/rendering.rb:100:in `_render_template'
actionpack (4.2.4) lib/action_controller/metal/streaming.rb:217:in `_render_template'
actionview (4.2.4) lib/action_view/rendering.rb:83:in `render_to_body'
actionpack (4.2.4) lib/action_controller/metal/rendering.rb:32:in `render_to_body'
actionpack (4.2.4) lib/action_controller/metal/renderers.rb:37:in `render_to_body'
These lines go on and on - im not sure if this is what you mean by 'backtrace'
来源:https://stackoverflow.com/questions/37041877/rails-4-associations-adding-team-mates-to-teams