没有路由匹配{:action =>"编辑",:controller =>" quiz_bs",:id => nil}缺少必需的密钥:[:id]

时间:2016-04-28 16:44:28

标签: ruby-on-rails events controller routes

我尝试链接以在我的rails应用中添加编辑测验链接,但我收到此错误:

No route matches {:action=>"edit", :controller=>"quiz_bs", :id=>nil} missing required keys: [:id]

我查看了类似的帖子(like this one),但他们的答案似乎并没有解决问题,即使他们提出了类似的情况。

我的application.html.erb代码的这一行显示错误:

  <li>
    <% if @user.quiz_bs == nil %>
       <%= link_to "Body Structure Quiz", quiz_bs_path %>
    <% else %>
       <%= link_to "Body Structure Quiz ✓", edit_quiz_b_path(id: @user.quiz_bs) %>
    <% end %>
  </li>

但链接也会出现在我的展会用户页面上:

  <h4>Body Structure</h4>
  <% if @user.quiz_bs == nil %>
    <p><%= link_to "Test Your Body Structure", new_quiz_b_path %></p>
  <% else %>
    <h3><%= @user.quiz_bs.bscode %></h3>
    <p><%= link_to "Retest Results", edit_quiz_b_path(id: @quiz_bs.id) %></p>
  <% end %>

这是我的quiz_bs_controller

class QuizBsController < ApplicationController
before_action :require_sign_in

def show
  @quiz_bs = QuizBs.find(params[:id])
end

def new
  @quiz_bs = current_user.quiz_bs || current_user.build_quiz_bs
end

def create
  @quiz_bs = QuizBs.new

  @quiz_bs.bs01 = params[:quiz_bs][:bs01]
  @quiz_bs.bs02 = params[:quiz_bs][:bs02]
  @quiz_bs.bs03 = params[:quiz_bs][:bs03]
  @quiz_bs.bs04 = params[:quiz_bs][:bs04]
  @quiz_bs.bs05 = params[:quiz_bs][:bs05]
  @quiz_bs.bs06 = params[:quiz_bs][:bs06]

  @quiz_bs.user = current_user

if @quiz_bs.save
  flash[:notice] = "Quiz results saved successfully."
  redirect_to user_path(current_user)
else
  flash[:alert] = "Sorry, your quiz results failed to save."
  redirect_to welcome_index_path
end
end

def edit
@quiz_bs = QuizBs.find(params[:id])

@quiz_bs.assign_attributes(quiz_bs_params)

if @quiz_bs.save
  flash[:notice] = "Post was updated successfully."
  redirect_to user_path(current_user)
else
  flash.now[:alert] = "There was an error saving the post. Please try again."
  redirect_to welcome_index_path
end
end

def update
@quiz_bs = QuizBs.find(params[:id])

@quiz_bs.assign_attributes(quiz_bs_params)

if @quiz_bs.save
  flash[:notice] = "Post was updated successfully."
  redirect_to user_path(current_user)
else
  flash.now[:alert] = "There was an error saving the post. Please try again."
  redirect_to welcome_index_path
end
end

private
def quiz_bs_params
params.permit(:bs01, :bs02, :bs03, :bs04, :bs05, :bs06)
end

end

相应的路线是:

        quiz_bs GET    /quiz_bs(.:format)             quiz_bs#index
                POST   /quiz_bs(.:format)             quiz_bs#create
     new_quiz_b GET    /quiz_bs/new(.:format)         quiz_bs#new
    edit_quiz_b GET    /quiz_bs/:id/edit(.:format)    quiz_bs#edit
         quiz_b GET    /quiz_bs/:id(.:format)         quiz_bs#show
                PATCH  /quiz_bs/:id(.:format)         quiz_bs#update
                PUT    /quiz_bs/:id(.:format)         quiz_bs#update

如果有任何帮助可以解决这个问题,我将非常感激!

2 个答案:

答案 0 :(得分:1)

application.html.erb中,更改为:

  <li>
    <% if current_user.quiz_bs.nil? %>
       <%= link_to "Body Structure Quiz", quiz_bs_path %>
    <% else %>
       <%= link_to "Body Structure Quiz ✓", edit_quiz_b_path(current_user.quiz_bs) %>
    <% end %>
  </li>
  • 我们希望将@user更改为current_user,因为此视图是整个网站的模板。无法保证@user将成为每个控制器中的全局变量(并且这可能不是您想要的)。使用current_user方法始终可用,并为您登录用户。
  • 注意:此处id:不是必需的,传递的模型将用作id。

users/show.html.erb中,更改为:

  <h4>Body Structure</h4>
  <% if @user.quiz_bs == nil %>
    <p><%= link_to "Test Your Body Structure", new_quiz_b_path %></p>
  <% else %>
    <h3><%= @user.quiz_bs.bscode %></h3>
    <p><%= link_to "Retest Results", edit_quiz_b_path(@user.quiz_bs) %></p>
  <% end %>
  • @quiz_b是您在users_controller中未定义的变量,就像您在quiz_bs_controller中所做的那样。您正在访问@user.quiz_bs,因此您希望链接到代码中的特定实例。

全局@quiz_b仅适用于已定义此变量的网页,该变量可以是quiz_b路径下的任何网页。

最后,在quiz_bs_controller

def quiz_bs_params
  params.require(:quiz_bs).permit(:bs01, :bs02, :bs03, :bs04, :bs05, :bs06)
end

将此方法与声明为:

的表单结合使用时
<%= form_for @quiz_bs do |f| %>

您需要:quiz_bs中的params,以便将参数范围限定为表单中正在编辑的模型;在这种情况下,@quiz_bs。否则,更新/创建测验时不会找到任何变量。

答案 1 :(得分:0)

这是你的问题:

No route matches {:action=>"edit", :controller=>"quiz_bs", :id=>nil} missing required keys: [:id]

您必须将编辑操作传递给ID,它是路由结构的一部分。在您的路线输出中,它甚至会向您显示:

/quiz_bs/:id/edit

没有该id,/:id / edit路由根本不起作用 - 这是一个Rails机制。

在你的Gemfile中:

group :development do
  gem 'pry-rails'
end

bundle install

然后在你的application.html.erb以上的逻辑中执行:

<% binding.pry %>
<% if @user.quiz_bs == nil %>
    <p><%= link_to "Test Your Body Structure", new_quiz_b_path %></p>
<% else %>
    <h3><%= @user.quiz_bs.bscode %></h3>
    <p><%= link_to "Retest Results", edit_quiz_b_path(id: @quiz_bs.id) %></p>
<% end %>

现在再次点击页面。 Pry将生成一个调试断点,允许您评估服务器当前运行的终端中的@user对象。我最好的建议是学会调试自己的代码 - 这是一种情况,如果没有提供对应用程序的访问权限,就无法提供很多上下文。