【问题标题】:Ruby on Rails - why i am facing possible unmatched constraints[:exercise_id]Ruby on Rails - 为什么我面临可能无法匹配的约束[:exercise_id]
【发布时间】:2018-09-25 09:58:02
【问题描述】:
def edit    
  @exercise = Exercise.find(params[:exercise_id])      
  @play = @exercise.plays.find(params[:id])    
end

def update    
  @exercise = Exercise.find(params[:exercise_id])
  @play = @exercise.plays.find(params[:id])    
  if @exercise.plays.update_attributes(play_params)       
    redirect_to @exercise_path      
  else    
    render action: :edit    
  end
end

正在渲染以显示所有创建的播放的部分视图具有

<p><%= play.name %></p>
<p><%= play.sets %></p>
<p><%= play.reps %></p>
<%= link_to "edit", edit_exercise_play_path( @exercise, play) %>

即plays_controller :edit, :update 方法,实际上我有两个类,一个是ExerciseController,另一个是PlaysControllerExerciseController has_many 播放,我正在渲染两个部分,一个用于创建播放和另一个在创建后渲染播放的部分的同一页面上显示该播放,但现在我想使用 edit_exercise_play_path 添加编辑功能,

<%= link_to "Edit", edit_exercise_play_path(@play) %>

在此之后我面临无与伦比的约束错误。谢谢

resources :exercises do
resources :plays
end

ExercisesController 中的 Show.html.erb

<h2>Your Workout Details </h2>
<p><%= @exercise.workout  %></p>
<p><%= @exercise.mode %></p>
<p><%= @exercise.date.strftime("on %A at %H:%M Dated as %d %B") %></p>
<p><%= @exercise.length %></p><br />
<h3> Games you Played </h3>
<%= render @exercise.plays %>
<h3>Add new Game </h3>
<%= render 'plays/form' %>
<%= link_to "Edit", edit_exercise_path %> |  |
<%= link_to "Destroy", exercise_path(@exercise), :confirm => "Are you 
sure?", :method => :delete %> | |
<%= link_to "Back", root_path %>

“日志” 2018-09-25 18:12:21 +0500 开始为 127.0.0.1 获取“/exercises/5” 由练习控制器处理#显示为 HTML 参数:{"id"=>"5"} 运动负荷 (0.0ms) SELECT "exercises".* FROM "exercises" WHERE "exercises"."id" = ?限制 ? [["id", 5], ["LIMIT", 1]] 渲染练习/show.html.erb 播放负载 (0.0ms) SELECT "plays".* FROM "plays" WHERE "plays"."exercise_id" = ? [["exercise_id", 5]] play/_play.html.erb [6 次] (14.0ms) 的渲染集合 渲染练习/show.html.erb (39.7ms) 在 136 毫秒内完成 500 内部服务器错误(ActiveRecord:0.0 毫秒)

【问题讨论】:

  • 你能在这里分享你的路线吗?
  • @PardeepDhingra 路线已添加。

标签: ruby-on-rails ruby-on-rails-4 ruby-on-rails-5.1


【解决方案1】:

根据您的路线,路径助手edit_exercise_play_path 也需要:exercise_id 键的值。但是你没有通过。将link_to 更改为以下应该可以解决您的问题

<%= link_to "Edit", edit_exercise_play_path(@exercise, @play) %>

更新:

根据我们在聊天中的讨论,link_to 编辑应如下所示

<%= link_to "Edit", edit_exercise_play_path(@exercise, play) %>

当您使用 &lt;%= render @exercise.plays %&gt; 渲染部分时,它会创建一个 play 变量。

nil:NilClass 的未定义方法 `model_name'

为此,您应该将&lt;%= render 'form' %&gt; 更改为&lt;%= render 'form' , exercise: @exercise, play: @play %&gt; 并在_form.html.erb 中将表单的第一行更改为&lt;%= simple_form_for ([exercise, play]) do |f| %&gt;

未定义的方法`update_attributes' 播放::ActiveRecord_Associations_CollectionProxy:0x0000000f69c710

这个错误的原因是你有@exercise.plays.update_attributes(play_params)的更新方法。这意味着您在收集时致电update_attributes。你应该改变

if @exercise.plays.update_attributes(play_params)

if @play.update_attributes(play_params)

【讨论】:

  • 我应用了这些更改,它向我显示了错误,没有路由匹配 {:action=>"edit", :controller=>"plays", :exercise_id=>"5", :id= >nil},缺少必需的键:[:id]
  • 我将其更改为, 有效,但创建了新的剧本。
  • @WaseyRaza 你能用点击编辑链接时出现的服务器日志更新问题吗?
  • @WaseyRaza 您在哪个视图页面中有该链接?您还可以使用该视图页面代码更新问题吗?
  • 好的,看问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-08-22
  • 1970-01-01
  • 2011-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-05
相关资源
最近更新 更多