在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
classattribute is incorrectly nested inside thedatahash
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 beset_application(missing 't')Applcation.find→ should beApplication.find(missing 'i')- Also, add a nil check in
add_more_documentsto 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
相关产品推荐
相关产品推荐

