0

嗨,我在show.html.erb中显示了估计预算

我正在使用这段代码:

<%= current_user.agency.project_procurement_management_plans.map{|p| p.total_estimated_budget.to_s} %>

但随后,输出变为:

["117000.5"]

我怎样才能分离里面的输出[""]

打赌是因为数组(?),但是我该如何防止呢?

同样,当我在控制器中执行此代码时:

def create
    @project_procurement_management_plan = ProjectProcurementManagementPlan.new(params[:project_procurement_management_plan])

    respond_to do |format|
      if @project_procurement_management_plan.save
        format.html { redirect_to @project_procurement_management_plan, notice:"#{@project_procurement_management_plan.code} was successfully created." }
        format.json { render json: @project_procurement_management_plan, status: :created, location: @project_procurement_management_plan }
      else
        #change me soon
        format.html { redirect_to  new_project_procurement_management_plan_path, alert:"#{@project_procurement_management_plan.errors.full_messages}"}
        format.json { render json: @project_procurement_management_plan.errors, status: :unprocessable_entity }
      end
    end
  end

错误消息位于[""]

例如

["Name can't be blank","The password is incorrect"]

等等。

任何解决方法将不胜感激。谢谢。

4

1 回答 1

2

是的,map确实返回一个包含所有元素的数组。如果您只有一个,则不应使用map

<%= current_user.agency.project_procurement_management_plans.first.total_estimated_budget.to_s %>

编辑

对于错误做

@project_procurement_management_plan.errors.full_messages.to_sentence

这将用逗号分隔您的错误

于 2013-03-12T00:55:07.357 回答