Uploading a file without a form - Rails - ruby-on-rails-4

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?

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 gem rails4-autocomplete 1 letter suggestions

I am using gem rails-jquery-autocomplete, but It shows suggestions only after 2 letters, is there a possibility to display suggestions for 1-letter long searching?
Yes there is! Just add 'min-length' => 1 in your search form like:
<%= form_tag terms_path, method: 'get' do %>
<%= autocomplete_field_tag :search, params[:search], autocomplete_term_phrase_terms_path, 'min-length' => 1 %>
<%= submit_tag "Search" %>
<% end %>

Rails: Conditional nested attributes in edit form

I have a model called Offer and another called PurchasinGroup
Offer has many PurchasingGroups
Offer accepts nested attributes for PurchasingGroups
While creating an offer you can add as many PurchasingGroups as you want.
PurchasingGroup has a boolean attribute called active.
while editing an Offer you can see all the created PurchasingGroups, however I want to let the user edit only the PurchasingGroups that are active, and do not display the inactive purchasing groups.
This is my edit action in offers_controller.rb:
def edit
#offer = Offer.find(params[:id])
end
And this is my form (only the part that matters):
<fieldset>
<legend>Purchasing groups</legend>
<%= f.fields_for :purchasing_groups do |builder| %>
<%= render partial: 'purchasing_group_fields', locals: { f: builder } %>
<% end %>
</fieldset>
In the edit form all the purchasing groups are being shown for edit, I want to show only those that are active I mean purchasing_group.active == true
How is the best way to do it?
<%= f.fields_for :purchasing_groups, #offer.purchasing_groups.where(active: true) do |builder| %>
<%= render partial: 'purchasing_group_fields', locals: { f: builder } %>
<% end %>
on the other hand, you can also add a association in your model
class Offer
has_many :active_purchasing_groups, class_name: "PurchasinGroup", -> { where(active:true) }
...
end
and then
<%= f.fields_for :active_purchasing_groups do |builder| %>
<%= render partial: 'purchasing_group_fields', locals: { f: builder } %>
<% end %>

Image not rendered in Sitecore GlassMapper

I am using GlassMapper in Sitecore Webforms, and i want to render image, but image is not rendering.
here is my code:
<% using (BeginRenderLink(x => x.LogoLink, isEditable:true))
{ %>
<%=RenderImage(x=>x.SiteLogo) %>
<% } %>
Are you sure, you using appropriate Model use like as:
<%= RenderImage(x => x.Image, isEditable: true) %>
if your are not able to fix your issue, try also
<%= GlassHtml.Editable(Model, x=> x.Image, x=> GlassHtml.RenderImage(x.Image)) %>
<%= Editable(x=> x.Image, x=> GlassHtml.RenderImage(x.Image)) %>

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