在rails form_tag中进行路由问题以收集项目 的ActionController :: UrlGenerationError
目标: 我在电影#index页面上展示了一系列电影。我在电影和用户之间创建了 movie_vote 关系,并希望在电影#index页面上显示每部电影的投票表单,以便用户可以为电影投票。如果用户之前已投票赞成该电影,则用户与电影之间的投票关系将被破坏。
控制器: 电影, MovieVotes, 用户
我看过了 ActionController::UrlGenerationError in Book#new
和http://edgeguides.rubyonrails.org/routing.html,
和Stripe: ActionController::UrlGenerationError | No route matches | missing required keys: [:id]但无济于事
设定:
配置/ routes.rb中
resources :movies
resources :users
resources :movie_votes
控制器/ 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
应用程序/模型/ movie_votes.rb
class MovieVotes < ActiveRecord::Base
belongs_to :voter_id, class_name: "Users"
belongs_to :voted_movie_id, class_name: "Movies"
end
应用程序/模型/ 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
应用程序/模型/ movies.rb
class Movie < ActiveRecord::Base
has_many :movie_votes, foreign_key: "voted_movie_id", dependent: :destroy
has_many :voters, through: :movies_votes
end
应用程序/视图/电影/ 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 %>
但是当运行rails时,我遇到了这个问题:
错误:
ActionController::UrlGenerationError in Movies#index
No route matches {:action=> "destroy", :controller=>"movie_votes", :method=>"DELETE"}
所以我尝试在movies / index.html.erb中的form_tag中切换方法和操作,使它们相同,但我仍然无法理解。
当我'搜索路线'时,我得到:
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
有没有人对如何在电影#index code中制定form_tag有任何指示?
答案 0 :(得分:0)
因为我可以看到你缺少这个应用程序的模型,所以关于一个用户投票给一些电影的时间是多少,你需要在模型中定义的这种东西看看这个链接
http://guides.rubyonrails.org/association_basics.html
所以,如果你问我这就是你所需要的:)