You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Rails应用中用CarrierWave添加多文档功能时遇语法错误求助

Let's break down and fix the syntax errors in your code step by step:

1. Syntax Error in application/show.html.erb Form

The immediate syntax error comes from misplaced parentheses in your form_for call. You closed the form_for parentheses too early, leaving the url: and method: options outside, which triggers the unexpected comma/label errors.

Original problematic line:

<%= form_for([@user, @application]), url: user_application_documents_path(@user, @application), method: :post do |f| %>

Corrected line:

<%= form_for([@user, @application], url: user_application_documents_path(@user, @application), method: :post) do |f| %>

Additionally, your delete link has two issues:

  • You're passing the loop index instead of the document ID to the path
  • The class attribute is incorrectly nested inside the data hash

Corrected delete link:

<%= link_to "Delete", user_application_document_path(@user, @application, document), 
      method: :delete, 
      data: { confirm: "Are you sure you want to delete this document?" },
      class: "btn btn-xs btn-danger" %>

2. Typos in documents_controller.rb

There are critical spelling mistakes that will break your controller logic:

  • set_applicaion → should be set_application (missing 't')
  • Applcation.find → should be Application.find (missing 'i')
  • Also, add a nil check in add_more_documents to avoid errors if no files are uploaded

Corrected controller code:

class DocumentsController < ApplicationController
  before_action :set_user
  before_action :set_application

  def create
    add_more_documents(documents_params[:documents])
    flash[:error] = "Failed uploading documents" unless @application.save
    redirect_back fallback_location: user_application_path(@user)
  end

  private
  def set_application # Fixed spelling
    @application = Application.find(params[:application_id]) # Fixed spelling
  end

  def set_user
    @user = User.find(params[:user_id])
  end

  def add_more_documents(new_documents)
    documents = @application.documents || [] # Add nil check
    documents += new_documents if new_documents.present?
    @application.documents = documents
  end

  def documents_params
    params.require(:application).permit({documents: []})
  end
end

Final Corrected show.html.erb Snippet

<p> <span class="text-label">Client Last Name:</span> <%= @application.user.lastname %> </p> 
<p> <span class="text-label">Client First Name:</span> <%= link_to @application.user.firstname, user_path(@user) %> </p> 
<p> <span class="text-label">Clinic:</span> <%= @application.clinic_name %> </p> 
<p> <span class="text-label">Applicant name:</span> <%= @application.applicant_name %> </p> 
<p> <span class="text-label">Application Status:</span> <%= @application.status %> </p> 

<% @application.documents.each_with_index do |document, index| %> 
  <%= image_tag(document.url) %> 
  <%= link_to "Delete", user_application_document_path(@user, @application, document), 
        method: :delete, 
        data: { confirm: "Are you sure you want to delete this document?" },
        class: "btn btn-xs btn-danger" %> 
<% end %> 

<%= form_for([@user, @application], url: user_application_documents_path(@user, @application), method: :post) do |f| %> 
  <div class="field"> <%= f.file_field :documents, multiple: true %> </div> 
  <div class="actions"> <%= f.submit "Add More Documents" %> </div> 
<% end %>

These fixes should resolve the syntax errors you're seeing, plus address some logical issues that would have caused problems down the line.

内容的提问来源于stack exchange,提问作者User7777

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 05:28:52