Hello all I have a Delete link that I'm trying to create with the following code:
<%= link_to 'Delete', element, method: :delete, data: { confirm: 'Are you sure?' } %>
However the resulting link is /element.id where the id is the integer id. How would I change that . to a / sorry I know this is very elementary but I couldn't find any documentation on it.
Try this
<%= link_to 'Delete', destroy_modelname_path(element), method: :delete, data: { confirm: 'Are you sure?' } %>
Replace modelname with the name of your model.
Run rake routes in terminal in your app to see all routes.
I guess the problem is in config/routes.rb, it should contain a reference to element, like:
resources :element
There is something wrong with routes file. It seems that your routing routine writes id in the place of :format.
Related
I am trying to implement a simple_form_for in my Rails app to attach many files on a ruby model. I want to upload several types of files (image, PDF, video, audio, .doc, ...). I use the ruby gem Cloudinary (with a free account) to store files in the cloud and Active Storage to link files with model instances. I use Rails 5.2.4.1 and Ruby 2.6.5 (see below my actual code)
Here is my model file:
class Substep < ApplicationRecord
has_many_attached :documents
end
Strong params controller :
class SubstepsController < ApplicationController
[...]
private
def substep_params
params.require(:substep).permit(documents: [])
end
end
Gemfile.rb :
gem 'cloudinary', '~> 1.12.0'
gem 'simple_form'
My API key in .env file :
CLOUDINARY_URL=............
config/storage.yml :
cloudinary:
service: Cloudinary
config/environments/development.rb :
config.active_storage.service = :cloudinary
app/views/substeps/_form.html.erb :
<%= simple_form_for(#substep) do |f| %>
<%= f.input :documents, as: :file, input_html: { multiple: true }, label: "Select files" %>
<%= f.submit "Send documents" %>
<% end %>
With this setup, I can manage images & PDF files easily.
However, I have read Cloudinary documentation (https://cloudinary.com/documentation/rails_integration) and simple_form_for documentation (https://github.com/heartcombo/simple_form) to find the right way to manage also video, audio & text files with the same setup, but I have failed so far.
I've find in the Cloudinary documentation that we can use an option ":resource_type => :auto" to manage different types of files automatically. I have already tried several configurations which have given nothing.
<%= f.input :documents, as: :file, input_html: { multiple: true, resource_type: :auto },... %>
<%= f.input :documents, as: :file, input_html: { multiple: true }, resource_type: :auto,... %>
<%= f.input :documents, as: :file, input_html: { multiple: true, resource_type: "auto" },... %>
<%= f.input :documents, as: :file, input_html: { multiple: true }, resource_type: "auto",... %>
<%= simple_form_for(#substep, resource_type: :auto) do |f| %>
<%= simple_form_for(#substep), resource_type: :auto do |f| %>
I don't success to find how I can add this option to my simple_form_for input. Someone can help me, please ?
EDIT 2020/07/13 :
I have found the solution thanks to another Dev. Here is his answer below.
"Hi! I just had the same problem as you and my solution was to update the cloudinary gem. In previous versions the cloudinary gem had "image" as the default for resource_type and it looks like you can't pass that option through active storage to cloudinary.
As soon as I removed that '~> 1.12.0' from the Gemfile and updated the gem to version 1.16, it started accepting the upload of other media types.
Probably the resource_type option changed to auto somewhere along the line."
I need to separate unique ids for some linking purpose, I just want to know how to make an unique id for link_to?
Like I am using:
<%= link_to "Edit", edit_question_path(question.id),method: :get, :remote => true, :id => #question.id%>
This didn't work then I tried:
<%= link_to "Edit", edit_question_path(question.id),method: :get, :remote => true, id: myBtn_<%=question.id%> %>
Nothing worked. Is there any solution?
Try with:
<%= link_to 'Edit',
edit_question_path(question.id),
method: :get,
remote: true,
id: "myBtn_#{question.id}" %>
You need to see what's the way to access a question variable, as instance or local one (I used a question, as you're doing with the path), and myBtn_<%=question.id%> won't work if you don't have defined a myBtn variable, even less if you try to include erb tags to print inside the erb open-close for the link_to.
I am trying to add a class to a button generated with rails button_to helper, but I cannot find a consistent answer to the question of how to incorporate the class into the declaration. I have:
<%= button_to("Logout", session_url, method: :delete, class:'waves-effect waves-light btn-large') %>
Try this
<%= button_to("Logout", session_url, method: :delete, :class => "btn btn-success") %>
This works fine for me
You need to split the "html options" from the "make the button_to" options - ie make two separate hashes eg:
<%= button_to("Logout", session_url, {method: :delete}, {class:'waves-effect waves-light btn-large'}) %>
Try this:
<%= button_to "Logout", session_url, method: :delete, form_class:"waves-effect waves-light btn-large" %>
I need to post through a link tag, however there is a problem with the link helper. Here is my link tag
// doctors.html.erb
<%= link_to "Ekle", [:add_doctor, #patient], method: :post %>
And my routes.rb
// routes.rb
get 'patients/:id/doctors' => 'patients#get_doctors'
post 'patients/:id/doctors' => 'patients#add_doctor'
I get the error
undefined method `add_doctor_patient_path' for #<#:0x007fd39283a4b0>
How should I use link helper to get rid of the problem?
This line in your routes.rb shows that you have a get_doctors route that accepts one argument, :id:
get 'patients/:id/doctors' => 'patients#get_doctors'
That means you should use the corresponding path helper, get_doctors_path, and pass it the Patient object:
<%= link_to "Ekle", get_doctors_path(#patient), method: :post %>
P.S. Your routes are puzzling, since you could accomplish the same thing by using resourceful routes, as recommended by the Rails Guides, instead of defining your own custom get and post routes, and in so doing save yourself a lot of trouble:
resources :patients do
resources :doctors
end
This would create e.g. patient_doctors_path and new_patient_doctor_path path helpers that would automatically route to your DoctorsController#get and DoctorsController#new methods, respectively, and it would enable you to use e.g. form_for [ #patient, :doctor ]. PatientsController is very much the wrong place to put logic for retrieving and modifying Doctors.
Just add an as: option on the routes.
// routes.rb
get 'patients/:id/doctors' => 'patients#get_doctors',as: :patient_get_doctor
post 'patients/:id/doctors' => 'patients#add_doctor', as: :patient_add_doctor
Then in the view:
// doctors.html.erb
<%= link_to "Ekle", patient_add_doctor_path(id: #patient.id), method: :post %>
I need to change this to HTML and have no idea how to put the delete route in a normal a tag, any ideas? Thanks
<li><%= link_to "Link", user_photo_pin_path(user_id: #user.id, photo_id: #photo.id, id: {{id}} ) , method: :delete, data: { confirm: 'Quieres borrar esto?'} %></li>
You can use something like "pry-rails" or "debugger" gems. Set up "debugger" method in your view and execute any of ActionView:Helpers like link_to or form_tag and see what html returned by this methods.