删除Rails 3.2映射中的[“”]额外字符

时间:2013-03-12 00:49:22

标签: ruby-on-rails ruby-on-rails-3

您好我在 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

错误消息位于[""]

E.g。

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

等等。

任何变通办法都将受到赞赏。感谢。

1 个答案:

答案 0 :(得分: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

这会将您的错误与逗号以及

分开