【问题标题】:Rails form for multiple objects, ActionController UrlGenerationError多个对象的Rails表单,ActionController UrlGenerationError
【发布时间】:2014-10-17 08:50:52
【问题描述】:

用于收集项目的 rails form_tag 中的路由问题 ActionController::UrlGenerationError

目标: 我有一系列 movies 我在 movies#index 页面上展示。我在电影和用户之间创建了 movie_vote 关系,并希望在 movies#index 页面上为每部电影显示一个投票表单,以便 用户 可以为这部电影投票。如果用户之前为电影投票,则用户与电影之间的投票关系被破坏。

控制器: 电影, 电影票, 用户

我看过 ActionController::UrlGenerationError in Book#new

和http://edgeguides.rubyonrails.org/routing.html,

和Stripe: ActionController::UrlGenerationError | No route matches | missing required keys: [:id] 但无济于事

设置:

config/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 

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 %>

但是在运行 rails 时,我遇到了这个问题:

错误:

ActionController::UrlGenerationError in Movies#index
No route matches {:action=> "destroy", :controller=>"movie_votes", :method=>"DELETE"}

所以我尝试在movies/index.html.erb中的form_tag中切换方法和动作,使它们相同,但我还是想不通。

当我'rake routes'时,我得到:

    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 代码中制定 form_tag 有任何指示吗?

【问题讨论】:

  • 尝试在您的form_tag 中将controller: "movie_vote" 更改为controller: "movie_votes"。
  • 嗨@Pavan,我有,但我得到了相同的结果。谢谢你的建议。

标签: ruby-on-rails arrays forms actioncontroller


【解决方案1】:

我可以看到您缺少此应用程序的模型,这完全是关于一个用户可以为某部电影投票多少次,而您需要在模型中定义的这种东西看看这个链接

http://guides.rubyonrails.org/association_basics.html

所以如果你问我这就是你需要的:)

【讨论】:

  • 嗨,我有。我已经包含在上面的信息中以供参考。谢谢!
猜你喜欢
  • 2015-11-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-04
  • 2017-03-29
  • 2015-12-30
  • 1970-01-01
相关资源
最近更新 更多