2

大家,早安,

我已经被这个错误困扰了一两天了。

我的应用程序是我的纺织品公司的一个小型“样品请求日志”,它只需要跟踪样品并能够编辑/提交/删除它们。每当我尝试更新或创建一个新请求时,我都会得到一个“x”的“未定义方法 `each':String” 其中 X 是在下拉列表中选择的选项(下拉列表是从另一个控制器填充的。)

请求控制器.rb:

class RequestsController < ApplicationController
before_action :set_request, only: [:show, :edit, :update, :destroy]

  # GET /requests
  # GET /requests.json
  def index
    if params[:tag]
     @requests = Request.tagged_with(params[:tag])
    else
     @requests = Request.all
     @requests = Request.order("request_date ASC")
    end
  end

 # GET /requests/1
 # GET /requests/1.json
 def show
 end

 # GET /requests/new
 def
 new
  @request = Request.new
  @customers = Customer.all
  @suppliers = Supplier.all
  @designers = Designer.all
  @statuses = Status.all
 end

# GET /requests/1/edit
 def edit
  @customers = Customer.all
  @suppliers = Supplier.all
  @designers = Designer.all
  @statuses = Status.all
 end

# POST /requests
# POST /requests.json
 def create
  @request = Request.new(request_params)
  @customers = Customer.all
  @suppliers = Supplier.all
  @designers = Designer.all
  @statuses = Status.all

   respond_to do |format|
    if @request.save
      format.html { redirect_to @request, notice: 'Request was successfully created.' }
      format.json { render action: 'show', status: :created, location: @request }
    else
      format.html { render action: 'new' }
      format.json { render json: @request.errors, status: :unprocessable_entity }
    end
  end
end

# PATCH/PUT /requests/1
# PATCH/PUT /requests/1.json
 def update

 @customers = Customer.all
 @suppliers = Supplier.all
 @designers = Designer.all
 @statuses = Status.all

respond_to do |format|
  if @request.update(request_params)
    format.html { redirect_to @request, notice: 'Request was successfully updated.' }
    format.json { head :no_content }
  else
    format.html { render action: 'edit' }
    format.json { render json: @request.errors, status: :unprocessable_entity }
  end
  end
end

# DELETE /requests/1
# DELETE /requests/1.json
 def destroy
  @request.destroy
 respond_to do |format|
  format.html { redirect_to requests_url }
  format.json { head :no_content }
 end
end

def tagged
  if params[:tags].present? 
    @requests = Request.tagged_with(params[:tags])
  else 
    @requests = Request.postall
  end  
end

private
  # Use callbacks to share common setup or constraints between actions.
  def set_request
    @request = Request.find(params[:id])
  end

# Never trust parameters from the scary internet, only allow the white list through.
def request_params
  params.require(:request).permit(:request_date, :request_smd_number, :request_brief_description, :request_description, :request_supplier, :request_colours, :request_date_required, :request_required_for, :request_designer, :request_comments, :request_confirmed_comp_date, :request_received, :request_received_date, :request_date_sent, :request_tracking_number, :request_confirmed_comp_date, :request_confirmed_comp_date2, :request_confirmed_comp_date3, :request_confirmed_comp_date4, :request_confirmed_comp_date5, :request_confirmed_comp_date6, :tag_list, :tags, :supplier, :customer, :designer, :name)
end

end

请求.rb

class Request < ActiveRecord::Base

validates_presence_of :request_smd_number, :request_date, :request_date_required, 
:request_required_for, :request_brief_description, :request_comments, :tags,
:request_supplier 

belongs_to :customer
belongs_to :designer
belongs_to :supplier
belongs_to :status
has_many :tags

accepts_nested_attributes_for :customer
accepts_nested_attributes_for :designer
accepts_nested_attributes_for :supplier
accepts_nested_attributes_for :status

acts_as_taggable_on :requests, :tags, :tag_list

end

我正在使用的表单可以在https://github.com/jackybigz/samplelog/blob/master/app/views/requests/_form.html.erb找到(链接到 git 而不是粘贴它以保留线程小的。)

每当我取出表格的最后一行'

<td class="span3"><%= collection_select :request, :tags, @statuses, :name, :name %></td>'

它工作正常并更新了请求,因此我将其范围缩小到了这一点,但是在转移了一堆关系和其他东西之后,尝试更新和不同的 gem 版本后,我遇到了困难和困难。

我对 ruby​​ 和 rails 相当陌生,所以任何帮助和建议都会有所帮助,谢谢。

PS 我的应用程序代码可能采用了与我应该的完全不同的方式,但它确实有效,所以我就是这样做的。

编辑这里是错误的跟踪。

activerecord (4.0.0) lib/active_record/associations/collection_association.rb:333:in `replace'
activerecord (4.0.0) lib/active_record/associations/collection_association.rb:42:in `writer'
activerecord (4.0.0) lib/active_record/associations/builder/association.rb:78:in `tags='
activerecord (4.0.0) lib/active_record/attribute_assignment.rb:42:in `public_send'
activerecord (4.0.0) lib/active_record/attribute_assignment.rb:42:in `_assign_attribute'
activerecord (4.0.0) lib/active_record/attribute_assignment.rb:29:in `block in assign_attributes'
activerecord (4.0.0) lib/active_record/attribute_assignment.rb:23:in `each'
activerecord (4.0.0) lib/active_record/attribute_assignment.rb:23:in `assign_attributes'
activerecord (4.0.0) lib/active_record/persistence.rb:229:in `block in update'
activerecord (4.0.0) lib/active_record/transactions.rb:326:in `block in with_transaction_returning_status'
activerecord (4.0.0) lib/active_record/connection_adapters/abstract/database_statements.rb:202:in `block in transaction'
activerecord (4.0.0) lib/active_record/connection_adapters/abstract/database_statements.rb:210:in `within_new_transaction'
activerecord (4.0.0) lib/active_record/connection_adapters/abstract/database_statements.rb:202:in `transaction'
activerecord (4.0.0) lib/active_record/transactions.rb:209:in `transaction'
activerecord (4.0.0) lib/active_record/transactions.rb:323:in `with_transaction_returning_status'
activerecord (4.0.0) lib/active_record/persistence.rb:228:in `update'
app/controllers/requests_controller.rb:67:in `block in update'
actionpack (4.0.0) lib/action_controller/metal/mime_responds.rb:363:in `call'
actionpack (4.0.0) lib/action_controller/metal/mime_responds.rb:363:in `retrieve_collector_from_mimes'
actionpack (4.0.0) lib/action_controller/metal/mime_responds.rb:189:in `respond_to'
app/controllers/requests_controller.rb:66:in `update'
actionpack (4.0.0) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (4.0.0) lib/abstract_controller/base.rb:189:in `process_action'
actionpack (4.0.0) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (4.0.0) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
activesupport (4.0.0) lib/active_support/callbacks.rb:413:in `_run__1386668850689043198__process_action__callbacks'
activesupport (4.0.0) lib/active_support/callbacks.rb:80:in `run_callbacks'
actionpack (4.0.0) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (4.0.0) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (4.0.0) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
activesupport (4.0.0) lib/active_support/notifications.rb:159:in `block in instrument'
activesupport (4.0.0) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.0.0) lib/active_support/notifications.rb:159:in `instrument'
actionpack (4.0.0) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (4.0.0) lib/action_controller/metal/params_wrapper.rb:245:in `process_action'
activerecord (4.0.0) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (4.0.0) lib/abstract_controller/base.rb:136:in `process'
actionpack (4.0.0) lib/abstract_controller/rendering.rb:44:in `process'
actionpack (4.0.0) lib/action_controller/metal.rb:195:in `dispatch'
actionpack (4.0.0) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
actionpack (4.0.0) lib/action_controller/metal.rb:231:in `block in action'
actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:80:in `call'
actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:80:in `dispatch'
actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:48:in `call'
actionpack (4.0.0) lib/action_dispatch/journey/router.rb:71:in `block in call'
actionpack (4.0.0) lib/action_dispatch/journey/router.rb:59:in `each'
actionpack (4.0.0) lib/action_dispatch/journey/router.rb:59:in `call'
actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:655:in `call'
rack (1.5.2) lib/rack/etag.rb:23:in `call'
rack (1.5.2) lib/rack/conditionalget.rb:35:in `call'
rack (1.5.2) lib/rack/head.rb:11:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/flash.rb:241:in `call'
rack (1.5.2) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.5.2) lib/rack/session/abstract/id.rb:220:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/cookies.rb:486:in `call'
activerecord (4.0.0) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.0.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:626:in `call'
activerecord (4.0.0) lib/active_record/migration.rb:369:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.0.0) lib/active_support/callbacks.rb:373:in `_run__1572239184247043471__call__callbacks'
activesupport (4.0.0) lib/active_support/callbacks.rb:80:in `run_callbacks'
actionpack (4.0.0) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/reloader.rb:64:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.0.0) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.0.0) lib/rails/rack/logger.rb:21:in `block in call'
activesupport (4.0.0) lib/active_support/tagged_logging.rb:67:in `block in tagged'
activesupport (4.0.0) lib/active_support/tagged_logging.rb:25:in `tagged'
activesupport (4.0.0) lib/active_support/tagged_logging.rb:67:in `tagged'
railties (4.0.0) lib/rails/rack/logger.rb:21:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
rack (1.5.2) lib/rack/runtime.rb:17:in `call'
activesupport (4.0.0) lib/active_support/cache/strategy/local_cache.rb:83:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/static.rb:64:in `call'
railties (4.0.0) lib/rails/engine.rb:511:in `call'
railties (4.0.0) lib/rails/application.rb:97:in `call'
rack (1.5.2) lib/rack/content_length.rb:14:in `call'
thin (1.5.1) lib/thin/connection.rb:81:in `block in pre_process'
thin (1.5.1) lib/thin/connection.rb:79:in `catch'
thin (1.5.1) lib/thin/connection.rb:79:in `pre_process'
thin (1.5.1) lib/thin/connection.rb:54:in `process'
thin (1.5.1) lib/thin/connection.rb:39:in `receive_data'
eventmachine (1.0.3) lib/eventmachine.rb:187:in `run_machine'
eventmachine (1.0.3) lib/eventmachine.rb:187:in `run'
thin (1.5.1) lib/thin/backends/base.rb:63:in `start'
thin (1.5.1) lib/thin/server.rb:159:in `start'
rack (1.5.2) lib/rack/handler/thin.rb:16:in `run'
rack (1.5.2) lib/rack/server.rb:264:in `start'
railties (4.0.0) lib/rails/commands/server.rb:84:in `start'
railties (4.0.0) lib/rails/commands.rb:78:in `block in <top (required)>'
railties (4.0.0) lib/rails/commands.rb:73:in `tap'
railties (4.0.0) lib/rails/commands.rb:73:in `<top (required)>'
bin/rails:4:in `require'
bin/rails:4:in `<main>'
4

1 回答 1

1

今天早上我找到了答案,带着新鲜的头脑来到这里,然后就明白了。

<td class="span3"><%= collection_select :request, :tags, @statuses, :name, :name %></td>

在集合选择中替换:tags:tag_list现在可以使用。

于 2013-08-08T08:09:54.587 回答