我创建了一个netsted表单和操作,但不断收到to_key错误

时间:2015-07-13 05:47:44

标签: forms ruby-on-rails-4 nested

我正在尝试按照教程进行操作。我使用与Blog Post和Blog Comment类型教程相同的方法,只是尝试一些不同的东西。我一直收到错误: 用于#的未定义方法`to_key'(必须删除第一个<)Agentinteraction :: ActiveRecord_Relation:0x007fa4d43ca138>

错误突出显示为:Agents#show中的NoMethodError

<% form_for @qrreviews, :url => create_qrreview_agents_path do |f| %>

路线:

 resources :agents do 
        collection do 
          post 'create_qrreview', to: 'agents#create_qrreview'
        end
    end

代理商展示行动:

 def show
     @qrreviews = Agentinteraction.where("agent_id = ?", @agent.id)
     @agentinteraction = Agentinteraction.new

  end

show.html.erb

 <p id="notice"><%= notice %></p>

  <%= @agent.name %>
  <%= @agent.email %>
  <%= @agent.team %>

<%= link_to 'Edit', edit_agent_path(@agent) %> |
<%= link_to 'Back', agents_path %>

QR Feedback:<br/><br/>
<%#= @comments.blog %>


<% @qrreviews.each do |qrreview| %>
  <%= qrreview.agent_id %><br>
  <%= qrreview.yes_or_no %><br>

<% end %>
<br/><br/>
<% form_for @qrreviews, :url => create_qrreview_agents_path do |f| %>
Category:
Interaction:
Act One Score:
Act Two Score: 
Act Three Score: 

Type of Interaction:

Training Needed?
Type of Training Needed?
<%= f.submit %>
<% end %>

1 个答案:

答案 0 :(得分:0)

  

Agentinteraction :: ActiveRecord_Relation的未定义方法`to_key':0x007fa4d43ca138

问题是你定义了@qrreviews = Agentinteraction.where("agent_id = ?", @agent.id),它返回 AR_Relation

尝试更改

@qrreviews = Agentinteraction.where("agent_id = ?", @agent.id)

@qrreviews = Agentinteraction.where("agent_id = ?", @agent.id).first

另外

<% form_for @qrreviews, :url => create_qrreview_agents_path do |f| %>

应该是

<%= form_for @qrreviews, :url => create_qrreview_agents_path do |f| %>