0

I'm trying to follow this tutorial to setup a modal containing a nested form in my Rails 4 app.

I have models for Project and Invite. The associations are:

Project has_many :invites
Invite belongs_to :project

In my views projects folder, I have made a partial called new_invitation.html.erb

<div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
    <h3 id="myModalLabel">Modal header</h3>
  </div>
  <div class="modal-body">
    **here comes whatever you want to show!**
  </div>
  <div class="modal-footer">
    <button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
    <button class="btn btn-primary">Save changes</button>
  </div>

I'm trying to link to that from from my project show page, which has:

<%= link_to 'Invite Team Mates', new_invitation_path,  {:remote => true, 'data-toggle' =>  "modal", 'data-target' => '#modal-window'}  %>
     <div id="modal-window" class="modal hide fade" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"></div>

In my app/javascripts folder, I have a file called new_invitation.js.erb, with:

$("#modal-window").html("<%= escape_javascript(render 'project/new_invitation') %>");

In my application js, I have:

//= require bootstrap/modal

(slightly different to the tutorial because I use rails 4 and bootstrap sass).

In my projects controller, I have:

def new_invitation
    respond_to do |format|
      format.html
      format.js
    end
end

I changed my routes from a put to a get action (although I don't understand this step):

 resources :projects do
    member do
    get "new_invitation" => "projects/new_invitation", as: :new_invitation
    end
    resources :invites
  end

There is a problem with the link path in the attempt above. I'm not sure why, but the error message suggests using:

new_invitation_project_path 

When I try that, I get an error that says:

undefined method `render' for #<#<Class:0x007fa2b2138bf0>:0x007fa2a04a1308>

I saw in the comments in the tutorial, that someone tried rewriting the js file as:

$("#modal-window").html("<%= escape_javascript(render :partial => 'project/new_invitation') %>");

I tried that but get the same error message. Can anyone see what I might need to do to replicate the success that the tutorial seems to have for other users?

Mel
  • 2,481
  • 26
  • 113
  • 273

1 Answers1

1

The problem is with the member route defined here.

resources :projects do
  member do
    get "new_invitation" => "projects/new_invitation", as: :new_invitation
end

end

The member route generated is

new_invitation_project GET  /projects/:id/new_invitation(.:format)  projects/new_invitation#new_invitation

The controller action projects/new_invitation#new_invitation doesn't even exist.

The mapping should be in the format controller#action. So, it should be

get "new_invitation" => "projects#new_invitation", as: :new_invitation

or even better,

get :new_invitation

Use rake routes | grep invitation to see the route generated.

In the new_invitation action, you're rendering a js response. So, rails will look for new_invitation.js.erb inside app/views/projects. You have to move your file from app/javascripts to the right location as mentioned above.

And there's another issue with your new_invitation.js.erb code. The _new_invitation.html.erb resides in views/projects/ directory. So, you should modify it to

$("#modal-window").html("<%= escape_javascript(render 'projects/new_invitation') %>");

otherwise, you'll get a Missing Template error since its looking for the template in the project directory. Make sure that you have _new_invitation.html.erb partial defined in app/views/projects directory.

To render the modal, you need to display the modal using the modal method.

$('#modal-window').modal('show');

Your new_invitation.js.erb should look like this.

$("#modal-window").html("<%= escape_javascript(render 'projects/new_invitation') %>");
$('#modal-window').modal('show');

Hope this helps!

Arun Kumar Mohan
  • 11,517
  • 3
  • 23
  • 44
  • Hi @Arun, I tried this, but I get this error: undefined method `render' for #<#:0x007fbff63d1090> – Mel Aug 29 '16 at 01:14
  • But i just found this: http://stackoverflow.com/questions/8370399/rails-js-erb-file-cannot-find-method-render – Mel Aug 29 '16 at 01:15
  • But - when I move the js file from app/assets/javascripts to views/projects - I can render the page, but the link to the modal just results in the whole screen becoming transparent grey. I can't do anything an no modal appears – Mel Aug 29 '16 at 01:22
  • I can see from the chrome inspector in the console, that there is an error that says: Missing template projects/new_invitation, application/new_invitation with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :haml, :jbuilder]}. – Mel Aug 29 '16 at 01:24
  • When I try to change the js.erb (now saved in views/projects folder), back to projects/new_invitation, that error message no longer displays in the console but the page doesnt load the modal – Mel Aug 29 '16 at 01:26
  • @Mel, Its looking for `new_invitation.js.erb` inside the `app/views/projects` directory. I have updated the answer. Have a look. – Arun Kumar Mohan Aug 29 '16 at 01:26
  • But @Arun - I did move the new_invitation.js.erb file to the views/projects folder (after I found the link I posted in the second line of this chat). – Mel Aug 29 '16 at 01:31
  • Where do I write this line: $('#modal-window').modal('show'); ? – Mel Aug 29 '16 at 01:33
  • @Mel, updated. If you're having trouble in understanding how this works, I would suggest you to read http://stackoverflow.com/questions/38875347/render-three-different-partials-depending-on-button-clicked/38939134#38939134 (I am not advertising) – Arun Kumar Mohan Aug 29 '16 at 01:36
  • Hi @Arun - thanks very much for the link to your other posts. That explains so many things for me. I tried updating the js file with the modal show line - but it still isn't working. I don't get a console error showing in the chrome inspector, but I just get a transparent grey screen when I click the link to the modal – Mel Aug 29 '16 at 01:51
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/122063/discussion-between-arun-kumar-and-mel). – Arun Kumar Mohan Aug 29 '16 at 01:52