Reputation: 351
Problem with routing in a rails form_tag for collection of items ActionController::UrlGenerationError
Goal: I have an array of movies that I show on movies#index page. I have created a movie_vote relationship between movies and users, and want to show a vote form for each movie on the movies#index page so that users can vote for the movie. If the user has voted for the movie before, the vote relationship between the user and movie is destroyed.
Controllers: Movies, MovieVotes, Users
I've looked at ActionController::UrlGenerationError in Book#new
and http://edgeguides.rubyonrails.org/routing.html,
and Stripe: ActionController::UrlGenerationError | No route matches | missing required keys: [:id] but to no avail
Setup:
config/routes.rb
resources :movies
resources :users
resources :movie_votes
controllers/movie_votes_controller.rb
def create
movie = Movie.find(params[:voted_movie_id])
current_user.vote!(@movie)
respond_to do |format|
format.html {redirect_to movies_path}
format.js
end
end
def destroy
movie = Movie.find(params[:voted_movie_id])
current_user.unvote!(@movie)
respond_to do |format|
format.html {redirect_to movies_path }
format.js
end
end
app/models/movie_votes.rb
class MovieVotes < ActiveRecord::Base
belongs_to :voter_id, class_name: "Users"
belongs_to :voted_movie_id, class_name: "Movies"
end
app/models/user.rb
class User < ActiveRecord::Base
has_many :movies
has_many :movie_votes, foreign_key: "voter_id", dependent: :destroy
has_many :voted_movies, through: :movie_votes
end
app/models/movies.rb
class Movie < ActiveRecord::Base
has_many :movie_votes, foreign_key: "voted_movie_id", dependent: :destroy
has_many :voters, through: :movies_votes
end
app/views/movies/index.html.erb
<% @movies.each do |m| %>
<% voter_result = m.voters.where('id' => current_user.id) %>
<% if (voter_result != [] && voter_result != nil) %>
<%=form_tag(controller: "movie_votes", action: "destroy", method: "delete") do |f| %>
<% f.hidden_field :voted_movie_id, :value => m.id %>
<% f.hidden_field :voter_id, :value => 4 %>
<%= f.submit %>
<% end %>
<% end %>
<% end %>
But when running rails s, I get this problem:
Error:
ActionController::UrlGenerationError in Movies#index
No route matches {:action=> "destroy", :controller=>"movie_votes", :method=>"DELETE"}
So I've tried switching the method and action in the form_tag in movies/index.html.erb, making them the same, but I still can't figure it out.
When I 'rake routes' I get:
movie_votes GET /movie_votes(.:format) movie_votes#index
POST /movie_votes(.:format) movie_votes#create
new_movie_vote GET /movie_votes/new(.:format) movie_votes#new
edit_movie_vote GET /movie_votes/:id/edit(.:format) movie_votes#edit
movie_vote GET /movie_votes/:id(.:format) movie_votes#show
PATCH /movie_votes/:id(.:format) movie_votes#update
PUT /movie_votes/:id(.:format) movie_votes#update
DELETE /movie_votes/:id(.:format) movie_votes#destroy
Does anyone have any pointers to how to formulate the form_tag in movies#index code?
Upvotes: 0
Views: 179
Reputation: 21
as I can see you are missing model for this app , it is all about how many time can one user vote for some movie and this kind of stuff you need to define in model have a look this link
http://guides.rubyonrails.org/association_basics.html
So if you ask me this is what you need :)
Upvotes: 0