0
votes

I follow the tutorial of Ryan Bates that Jquery file upload but the example was only 1 model. I tried to make a version for my application that serviceproviders has_many photos and photos belongs_to serviceprovider.

This is my new.html.erb

<h1>Add a Clearance</h1>

<%= form_for Clearance.new do |f| %>
    <%= f.label :avatar, "Clearances" %>
    <%= f.file_field :avatar, multiple: true, name: "clearance[avatar]" %> 
<% end %>

Clearance controller

def new @serviceprovider = Serviceprovider.find(params[:serviceprovider_id] @clearance = @serviceprovider.clearances.new end

def create
    @clearance = Clearance.create(clearance_params)
end

And I got an error undefined method "clearances_path" How can I upload a photo with associated models. Thank you!


**

Update

**

I'm trying to create a galleries of photo of clearances for Serviceproviders. For example a serviceproviders has a Police clearance, NBI Clearance and any clearances that may notify that he had no any criminal cases. Now I can upload the photo using the jquery-fileupload (thanks for @OlivierLance for helping me) but now I want every time I upload the photo it will automatically display. I new to rails and I have a hard time to learn some gems because as I said I'm not familiar in rails. I already the solutions that I will reload the page so that it will display every time I upload but nothings happen. The page had a infinite loop redirecting the page.

def new
        @serviceprovider = Serviceprovider.find(params[:serviceprovider_id])
        @clearance = @serviceprovider.clearances.new

        @clearances = @serviceprovider.clearances
                redirect_to new_serviceprovider_clearance_url(@serviceprovider)
        end
    end 

def create
    @serviceprovider = Serviceprovider.find(params[:clearance][:serviceprovider_id])
    @clearance = @serviceprovider.clearances.create(clearance_params)
end

How can I achieve that?

1

1 Answers

0
votes

Disclaimer: this answer is only valid if you intend to have your new page handle a single Clearance addition to a specific Serviceprovider instance

Let's clarify the cause of the error you get: what Rails is telling you, is that you're trying to create a form for a Clearance object but it doesn't know where to send the creation POST request to (the URL that would end up in the action attribute of the generated <form> tag).

You will have to add routes for Rails to match URLs to actions of your ClearancesController. The minimal addition you should make in your routes.rb file is:

resources :clearances, only: [:create]

I assume you already have a nested route within the service providers' routes to display the new clearance form.

This new route will create a new route helper, clearances_path, as it is described in the Rails routing guide.
Now that this route exists, Rails will be able to generate the form for a new Clearance object.

However there are a few fixes you need to apply to your current code.

First, your form is built using Clearance.new. Clearance.new will return a new, naked, disowned Clearance object (poor one!). What you need though, is a new object that belongs to the clearances collection of the Serviceprovider you're adding it to.

Luckily enough, you have created one of these in the new action of your controller. Why not use it? :)

# new.html.erb
<h1>Add a Clearance</h1>

<%= form_for @clearance do |f| %>
    <%= f.hidden_field :serviceprovider_id %>
    <%= f.label :avatar, "Clearances" %>
    <%= f.file_field :avatar, multiple: true, name: "clearance[avatar]" %> 
<% end %>

And why does it matter? Well because this Clearance object has been initialized with a non-null serviceprovider_id (assuming that's the name of your foreign key). Note that I have added an hidden field that will hold this id within the form.

With all this in place, when you submit your form, it'll go to your create action and create a new Clearance object within your database that will be correctly associated to the Serviceprovider you wanted.

Considering the content of your question, that's how far I can answer. I guess you got the file upload/saving right by following Ryan Bates' tutorial. If not, you'll need to add details about what you did/did not do and what's actually failing there!

(PS: you should definitely rename your Serviceprovider class to ServiceProvider) Update

new is the action that will display your form and your already uploaded images. create is the action that is called when you submit the form. What you want, is go back to the new page once a clearance has been added: move your redirect_to to the end of the create action instead of new.