Rails 4 - link_to with confirm in Rails - ruby-on-rails-4

I'm trying to customize my dialog confirm the link_to in rails 4. But I do not understand why he rides the html as "confirm", and should be "data-confirm"!
I've tried a number of ways, and it is always generated "confirm", like this:
<%= link_to 'Delete', user, :method => :delete, :confirm => 'Are you sure?' %>
<a confirm="Are you sure?" rel="nofollow" data-method="delete" href="/user/13">Delete</a>
I followed this tutorial:
http://thelazylog.com/custom-dialog-for-data-confirm-in-rails/
and i used the example in tutorial, but also doesn't work

Confirm has been deprecated in rails 4.
So use new syntax:
<%= link_to "title", "#", data: {confirm: "Are you sure!"} %>

To get the same behavior in rails 7 you should use turbo_confirm key instead:
<%= link_to 'Delete', user,
data: {turbo_method: :delete, turbo_confirm: 'Are you sure?'} %>

Related

No route matches [POST] "/basic_pages/basic_pages/home"

So I would simply like to post to my root. My problem is that if I try to do it the second time (first time works fine) I am posting /basic_pages/basic_pages/home instead of /basic_pages/home. Why does that happen and how do I fix this?
home.html.erb:
<h1>BasicPages#home</h1>
<p>Find me in app/views/basic_pages/home.html.erb</p>
<%= form_tag ('basic_pages/home') do %>
<%= text_field_tag :my_input %>
<%= submit_tag "Send input" %>
<% end %>
routes.rb
Rails.application.routes.draw do
root 'basic_pages#home'
post 'basic_pages/home'
get 'about' => 'basic_pages#about'
end
Hope this provides all the information necessary.
Instead of using form_tag ('basic_pages/home') use form_tag ('/basic_pages/home'). When you have posted the url is getting changed to /basic_pages/home, and then the form is again posting to /basic_pages/basic_pages/home, because of the relative path given to the form.
Try this
Rails.application.routes.draw do
post '/' => "basic_pages#home", as: "root"
get 'about' => 'basic_pages#about'
end
home.html.erb:
<%= form_tag ('/') do %>
<%= text_field_tag :my_input %>
<%= submit_tag "Send input" %>
<% end %>

(Rails 4) Acts_as_taggable_on: using a model other than "Tag"

I am trying to implement the acts_as_taggable_on gem. In my set up I have a Model called Discipline which is pre-populated with about 40 names.
I also have a Model Micropost which I want to tag - using a select box containing all the names in the disciplines database. I thought that I could call the acts_as_taggable_on the Model I wanted - in this case Disciplines but its not working with my current set up.
class Micropost < ActiveRecord::Base
acts_as_taggable
acts_as_taggable_on :disciplines
end
Here is the form......
<%= simple_form_for(#micropost) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.input :tag_list, :as => :select,
:multiple => :true,
:collection => ActsAsTaggableOn::Tag.pluck(:name) %>
<%= f.text_area :content, placeholder: "What is your question?", :style => "height:75px;" %>
<%= f.submit "Post", class: "btn btn-primary" %>
<% end %>
I can tell from the documentation that there is a way to do this....but I guess I am just not getting it. Any suggestions?
I don't think you can use acts_as_taggable_on using a model other than the default Tag and Taggings models.
Alternative Approach #1
Seed your database with the pre-populated 40 Tags containing your discipline names.
Alternative Approach #2
Use bitmask_attributes for your 40 disciplines.
For example, in my application I have:
bitmask :instruments, as: [:others, :guitar, :piano, :bass, :mandolin, :banjo,
:ukulele, :violin, :flute, :harmonica, :trombone,
:trumpet, :clarinet, :saxophone, :viola, :oboe,
:cello, :bassoon, :organ, :harp, :accordion, :lute,
:tuba, :ocarina], null: false

Indicate that an uploaded file is present in edit form -- paperclip

In my current solution, I am able to put a checkbox in the edit form so that users can delete attachment. However, there is no indication for the user that a file has been uploaded, the name of that file, etc. so that he can decide whether to delete.
Right now the form look like this. The first material is an existing one, the next 3 are due to
def edit
#post = Post.find(params[:id])
3.times { #post.post_materials.new }
end
As you can see, it's very hard to distinguish between them. Ideally, I want the first material file name to appear somehow.
<%= form_for #post, :html => { :multipart => true } do |f| %>
<%= f.label :title %>
<%= f.text_field :title %>
Materials:
<ul>
<%= f.fields_for :materials, :html => { :multipart => true } do |materials_form| %>
<li>
<%= materials_form.label :asset %>
<%= materials_form.file_field :asset %>
<%= materials_form.label :_destroy, class: "checkbox inline" do %>
Remove attachment <%= materials_form.check_box :_destroy %>
<% end %>
</li>
<% end %>
</ul>
<%= f.submit "Submit", class: "btn btn-large" %>
<% end %>
Running paperclip's generator creates a migration to add 4 attributes on your model, as you can see here. These attributes are:
<attachment>_file_name
<attachment>_file_size
<attachment>_content_type
<attachment>_updated_at
So, If you ran the generator this way: rails generate paperclip post_material asset, on your PostMaterial model, you will have these attributes:
asset_file_name
asset_file_size
asset_content_type
asset_updated_at
Then, on your code you can do something like this:
if materials_form.object.asset.exists? #object represents the current post_material instance
#show a label with object.asset_file_name
else
#render materials_form.file_field :asset
end

RubyonRails Guide confusion over link creation in Ruby

I am a beginner to Rails. Was working along with Railsguide for Rails 4.
One confusion i have is:
for adding a link , somewhere it is written like:
<h1>Hello, Rails!</h1>
<%= link_to "My Blog", controller: "posts" %>
whereas somewhere its like
<%= link_to 'New post', new_post_path %>
Please clarify the difference.
Good question. Both of these are link helpers, and both are resource-oriented (check out the Rails UrlHelper docs for more information).
The first one will render a link that is associated with the particular controller:
<%= link_to "My Blog", controller: "posts" %>
<a href='/posts'>
The second will render the Rails path specific to creating a new Post object (this is also resource-oriented). Check out section 2.3 in the Rails routing guide:
<%= link_to 'New post', new_post_path %>
<a href='/posts/new'>

Uploading a file without a form - Rails

Ruby 2.1
Rails 4.1
It should be pretty simple to upload a file without using any gems. I would include in a form something like this:
<% form_for #upload, :multipart => true do |f| %>
<%= f.file_field :uploaded_file %>
<%= f.submit "Upload file" %>
<% end %>
What I want to do is upload the file through a single menu action. How do I go about doing this?