Skip to content

Commit ae9c477

Browse files
committed
got Create / Read working for Roles
1 parent 77397e9 commit ae9c477

File tree

12 files changed

+87
-5
lines changed

12 files changed

+87
-5
lines changed

app/controllers/actors_controller.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
class ActorsController < ApplicationController
2-
around_filter Neo4j::Rails::Transaction, :only => [:edit, :update, :destroy, :create]
32

43
def index
54
@actors = Actor.all
@@ -44,5 +43,10 @@ def destroy
4443
@actor.destroy
4544
redirect_to(actors_url)
4645
end
46+
47+
def add_role
48+
@actor = Actor.find(params[:id])
49+
@movies = Movie.all
50+
end
4751
end
4852

app/controllers/movies_controller.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
class MoviesController < ApplicationController
2-
around_filter Neo4j::Rails::Transaction, :only => [:edit, :update, :destroy, :create]
32

43
def index
54
@movies = Movie.all
@@ -44,5 +43,10 @@ def destroy
4443
@movie.destroy
4544
redirect_to(movies_url)
4645
end
46+
47+
def add_role
48+
@movie = Movie.find(params[:id])
49+
@actors = Actor.all
50+
end
4751
end
4852

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class RolesController < ApplicationController
2+
3+
def show
4+
@role = Role.find(params[:id])
5+
end
6+
7+
def new
8+
@movie = Movie.find(params[:movie_id])
9+
@actor = Actor.find(params[:actor_id])
10+
@role = Role.new
11+
end
12+
13+
def create
14+
@movie = Movie.find(params[:movie_id])
15+
@actor = Actor.find(params[:actor_id])
16+
@role = @actor.roles_rels.connect(@movie, params[:role])
17+
@role.save
18+
redirect_to role_path(@role.id)
19+
end
20+
end

app/models/role.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
11
class Role < Neo4j::Rails::Relationship
22
property :as
3+
4+
# this is necessary to be able to make forms with the form_for helper
5+
def to_key
6+
persisted? ? [id] : nil
7+
end
8+
9+
# this is for the routing helpers
10+
def to_param
11+
persisted? ? neo_id.to_s : nil
12+
end
313
end

app/views/actors/add_role.html.erb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<h2>New Role for <%= @actor.name %></h2>
2+
<div><%= @actor.name %> acted in...</div>
3+
<% @movies.each do |movie| %>
4+
<div><%= link_to movie.title, roles_path(@actor, movie) %></div>
5+
<% end %>
6+

app/views/actors/show.html.erb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
<table>
2121
<% @actor.roles_rels.each do |r| %>
2222
<tr>
23-
<td><%= sanitize r.as %> in <%= link_to(sanitize(r.end_node.title), movie_path(r.end_node)) %>
23+
<td><%= link_to r.as, role_path(r) %> in <%= link_to(r.end_node.title, movie_path(r.end_node)) %>
2424
</tr>
2525
<% end %>
2626
</table>
27+
28+
<%= link_to 'New Role', add_role_actor_path(@actor) %>

app/views/movies/add_role.html.erb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<h2>Add an Actor for <i><%= @movie.title %></i></h2>
2+
<% @actors.each do |actor| %>
3+
<div><%= link_to actor.name, new_role_path(actor, @movie) %></div>
4+
<% end %>

app/views/movies/show.html.erb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,5 @@
2323
</tr>
2424
<% end %>
2525
</table>
26+
27+
<%= link_to 'New Role', add_role_movie_path(@movie) %>

app/views/roles/_form.html.erb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<%= form_for(@role) do |f| %>
2+
<% if @role.errors.any? %>
3+
<div id="error_explanation">
4+
<h2><%= pluralize(@actor.errors.count, "error") %> prohibited this actor from being saved:</h2>
5+
6+
<ul>
7+
<% @role.errors.full_messages.each do |msg| %>
8+
<li><%= msg %></li>
9+
<% end %>
10+
</ul>
11+
</div>
12+
<% end %>
13+
14+
<%= f.text_field :as %>
15+
16+
<div class="actions">
17+
<%= f.submit %>
18+
</div>
19+
20+
<% end %>

app/views/roles/new.html.erb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<h2>New Role</h2>
2+
<div><%= @actor.name %> acted in <i><%= @movie.title %></i> as...</div>
3+
<%= render "form" %>

0 commit comments

Comments
 (0)