file_field upload button appearing multiple times on edit form - ruby-on-rails

I have this code in my _form.html.erb file:
<%= f.fields_for :request_attachments do |ra| %>
<div class="row westmontTextMuseo3" id="uploader">
<div class="col-xs-12">
<label class="btn btn-info"> Upload Files
<%= ra.file_field :request_attachment_file, :multiple => true, name: "request_attachments[file][]", :style => "display: none", type: "file" %>
<%= ra.hidden_field :file_cache %>
</label>
<%= link_to(ra.object.file.url.to_s.split('/')[-1], ra.object.file.url) %>
</div>
</div>
<% end %>
I can see why the Upload Files button appears. It seems like I want it outside the scope of the nested form, but I don't know how to do that.
My goal is to place the button once after it has provided links to each of the already attached files.
Any help would be great, thanks.
Edit: Other code to see if we can get it to work.
<%= f.fields_for :request_attachments do |ra| %>
<%= link_to(ra.object.file.url.to_s.split('/')[-1], ra.object.file.url) %>
<% end %>
<div class="row westmontTextMuseo3" id="uploader">
<div class="col-xs-12">
<label class="btn btn-info"> Upload Files
<%= #request.request_attachments.file_field :request_attachment_file, :multiple => true, name: "request_attachments[file][]", :style => "display: none", type: "file" %>
<%= #request.request_attachments.hidden_field :file_cache %>
</label>
</div>
</div>
But this fails with an undefined method file_field' for #<RequestAttachment...

This seemed to have worked
<%= f.fields_for :request_attachments do |ra| %>
<div class="row">
<div class="col-xs-12">
<%= link_to(ra.object.file.url.to_s.split('/')[-1], ra.object.file.url) %>
</div>
</div>
<% end %>
<div class="row westmontTextMuseo3" id="uploader">
<div class="col-xs-12">
<label class="btn btn-info"> Upload Files
<%= file_field_tag :request_attachment_file, :multiple => true, name: "request_attachments[file][]", :style => "display: none", type: "file" %>
<%= hidden_field_tag :file_cache %>
</label>
</div>
</div>
I assumed since the attributes were nested it wouldn't work, but it did work.

Related

Adding multiple images on a page with CRUD

I want to add several different images with a crud to make some "work pages" but i struggle with the way to do this. Do I have to update my model or how i can specify that each adding field is for a section of my view? I'm using cloudinary to host the images.
For now when I add an image its all the same but I wanted to add differents ones at each spot.
My work#new view :
<div class="container text center">
<%= simple_form_for(#work) do |f| %>
<div class="row justify-content-center">
<div class="col-md-6 col-sm-12">
<h1 class="heading-02" style="padding: 2rem 0rem;">New Work</h1>
<div class="work-new-section">
<h2 class="sub-heading-03">Header</h2>
<%= f.input :name %>
<%= f.input :description %>
<%= f.input :photo, as: :file %>
</div>
<div class="work-new-section">
<h2 class="sub-heading-03">Tasks</h2>
<%= f.input :task_title %>
<%= f.input :tasks %>
<%= f.input :photo, as: :file %>
</div>
<div class="work-new-section">
<h2 class="sub-heading-03">Content#1</h2>
<%= f.input :content1_title %>
<%= f.input :content1 %>
<%= f.input :photo, as: :file %>
</div>
<div class="work-new-section">
<h2 class="sub-heading-03">Focus</h2>
<%= f.input :focus_title %>
<%= f.input :focus %>
</div>
<div class="work-new-section">
<h2 class="sub-heading-03">Content#2</h2>
<%= f.input :content2_title %>
<%= f.input :content2 %>
<%= f.input :photo, as: :file %>
</div>
<%= f.submit 'Create a new Work', :class => 'button-accent mb-4' %>
</div>
</div>
<% end %>
</div>
Work#new
My Work model :
class Work < ApplicationRecord
has_one_attached :photo
end
For the work#show is use some partials with the same behaviour in it:
<div class="container">
<div class="row">
<div class="col-md-4 col-sm-12">
<%= cl_image_tag #work.photo.key, :class=> "work-image", :width => 420, :height => 236, :crop => "fill" %>
</div>
<div class="col-md-4 col-sm-12">
<h2 class="sub-heading-03"><%= #work.content1_title %></h2>
<p class="ui-regular-01"><%= #work.content1 %></p>
</div>
</div>
</div>
Work#show
How could I specify one particular photo to each section? (task, content#1, content#2 etc...)
Thank you very much for your time!
I tried to change my new view but i have the impression that i omit something that i don't understand very well.

Rspec/Capybara test post missing parameter from form but parameter is submmitted fine in dev when testing manually

One of my tests is creating a form post via javascript that is dropping the first input field from the form. The tests are obviously failing because of this. When deugging via binding.pry, it looks like a js post is dropping the first parameter [address_by_user] when posting it to my route. This parameter is posted just fine when testing via the ui in localhost but no longer works with rspec. Any idea what could cause this? Tests were passing as of two days ago and the forms haven't been changed. I tried restarting my computer in case it was a zombie process or something being cached, but the behavior continues. Any thoughts?
The form that is submitted via javascript using the following code:
var form_id = "#form-step" + currentIndex;
var my_form = $(form_id);
var url = $(form_id).attr('action');
var form_data = my_form.serialize();
console.log(form_data);
var submission = $.post(url, form_data);
The form looks like this and is submitted after clicking the "Next" button in a wizard, that works fine.
<%= form_with(url:"add_property", scope: :property, class: "form", id:"form-step0") do |f| %>
<div class="form-body" id="step0form">
<h4 class="form-section"><i class="ft-location"></i> <%= t(:property_details) %></h4>
<%= render 'shared/error_messages', object: f.object %>
<div class="row">
<div class="form-group pac-card col-md-8 offset-md-1 required" id='pac-card'>
<div class="field" id='pac-container'>
<%= f.label :address_by_user, t(:address), class:"label-control" %>
<%= f.text_field :address_by_user, :required => true, class: "form-control pac-input google-autocomplete", id:"pac-input" %>
</div>
</div>
<div class="form-group form-row col-md-2">
<div class="field">
<%= f.label :unit_number %>
<%= f.text_field :unit_number, class: "form-control" %>
</div>
</div>
</div>
<div class="form-group form-row">
<div class="field col-md-2 offset-md-1">
<%= f.label :bedrooms %>
<%= f.number_field :bedrooms, :required => true, class: "form-control", in: 1...20 %>
</div>
<div class="field col-md-2 offset-md-1">
<%= f.label :bathrooms %>
<%= f.number_field :bathrooms, class: "form-control", in:1...10 %>
</div>
<div class="field col-md-2 offset-md-1">
<%= f.label :accommodates %>
<%= f.number_field :accommodates, class: "form-control", min: "0" %>
</div>
</div>
<div class="form-group form-row">
<div class="field col-md-2 offset-md-1"><%=label_tag(t(:property_type))%></div>
</div>
<div class="form-group form-row" data-toggle="buttons">
<div class="btn-group col-md-12">
<div class="field col-md-2 offset-md-1">
<%= f.label :room_type, class: 'text-center btn btn-default active' do %>
<%= image_tag('house-icon.png') %>
<br>
<%= f.radio_button :room_type, "entire_place", checked: true %>
<span>Entire Place</span>
<% end %>
</div>
<div class="field col-md-2 offset-md-1">
<%= f.label :room_type, class: 'text-center btn btn-default' do %>
<%= image_tag('room.png') %>
</br>
<%= f.radio_button :room_type, "private_room" %>
<span>Private Room</span>
<% end %>
</div>
<div class="field col-md-2 offset-md-1">
<%= f.label :room_type, class: 'text-center btn btn-default' do %>
<%= image_tag('people.png') %>
</br>
<%= f.radio_button :room_type, "shared_room" %>
<span>Shared Room</span>
<% end %>
</div>
</div>
</div>
<div id="hidden-fields">
<%= f.hidden_field(:latitude, id: 'latitude') %>
<%= f.hidden_field(:longitude, id: 'longitude') %>
<%= f.hidden_field(:street_number, id: 'street_number') %>
<%= f.hidden_field(:street_name, id: 'route') %>
<%= f.hidden_field(:city, id: 'locality')%>
<%= f.hidden_field(:state, id: 'administrative_area_level_1') %>
<%= f.hidden_field(:country, id: 'country')%>
</div>
</div>
<% end %>
When tested directly from the site via localhost, all parameters post fine and I get this: (as expected)
<ActionController::Parameters {"address_by_user"=>"Pura Uvita Vacation Home, Puntarenas Province, Uvita, Costa Rica", "unit_number"=>"", "bedrooms"=>"2", "bathrooms"=>"2.0", "accommodates"=>"6", "room_type"=>"entire_place", "latitude"=>"9.1839769", "longitude"=>"-83.72444630000001", "street_number"=>"", "street_name"=>"", "city"=>"Uvita", "state"=>"Provincia de Puntarenas", "country"=>"Costa Rica"} permitted: false>
When the parameters are obtained via binding.pry from the rspec flow, the following is the result:
<ActionController::Parameters {"unit_number"=>"", "bedrooms"=>"2", "bathrooms"=>"2", "accommodates"=>"6", "room_type"=>"entire_place", "latitude"=>"", "longitude"=>"", "street_number"=>"", "street_name"=>"", "city"=>"", "state"=>"", "country"=>""} permitted: false>
The two most likely causes of this are
The fields are no longer actually being filled in - look at the browser while the tests are running and see if the form is actually being correctly filled in.
You have an error in another JS file causing the JS you think is processing the form not to run. This can happen because JS assets are concatenated in the test environment, allowing an error in on JS file to prevent any files concatenated after it not to be processed.

How to add in multiple options for checkbox in Ruby on Rails?

This is likely a super easy answer for a Ruby on Rails expert. I have a form and need to add in a checkbox that has multiple items. I've been messing with the following code for a lot longer than I'd like to admit:
<%= form_for :lead, url: something, html: {id: 'product-form'} do |f|%>
<div class="row">
<div class="col-md-12">
<div class="form-group">
<%= f.label :product%>
<%= f.check_box :product, {multiple:true}, "option", "option2", :class => 'form-control'%>
</div>
</div>
</div>
<% end %>
With this code I get the error "wrong number of arguments (5 for 1..4)".
Basically I just want someone to be able to pick multiple options. I've also tried the following:
<div class="row">
<div class="col-md-12">
<div class="form-group">
<%= f.label :product%>
<%= f.check_box :option1, "Option1" :class => 'form-control'%>
<%= f.check_box :option2, "Option2", :class => 'form-control'%>
</div>
</div>
</div>
And I get the delightful "undefined method `merge' for "Option1":String". What am I missing to put the values in associated with the label?
I use Rails 5.1.6 and this is how I achieved adding multiple options for checkbox. I also placed it in a dropdown.
In the migration file:
t.string :skills
In my controller "tasks_controller.rb":
def task_params
params.require(:task).permit(:work_type, :title, :post, {skills: []})
end
In my model "task.rb" I did:
validates :skills, presence: true
serialize :skills, JSON
I created a module mobilephones_data.rb in directory "app/model/concerns/" which holds all the options of the checkbox as an array that can be edited easily.
In app/model/concerns/mobilephones_data.rb I wrote:
module Mobilenphones_Data
Activities = [
'Amazon Kindle', 'Blackberry', 'iPad', 'iPhone', 'Mobile Phone', 'Nokia',
'Palm', 'Samsung'
]
end
This will put all data from module's array into the checkbox drop-down as checkbox options. In My form I did:
<div class="card">
<a class="card-link card-header" data-toggle="collapse" href="#collapseOne">
Mobile Phones
</a>
<div id="collapseOne" class="collapse" data-parent="#accordion">
<div class="card-body">
<% Mobilenphones_Data::Activities.each do |activity| %>
<div id="skill_list" class="col-lg-3 col-md-4 col-sm-12 col-12">
<%= f.check_box :skills, { multiple: true }, activity, false %>
<%= activity %>
</div>
<% end %>
</div>
</div>
</div> <!--bottom-->
</div>
In my view "show.html.erb":
<% #name_of_model.skills.each do |skill| %>
<%= skill %>
<% end %>
For posterity sake:
<div class="row">
<div class="col-md-12">
<div class="form-group">
<%= f.label "Select a Product" %><br />
<%= f.check_box(:option) %>
<%= f.label(:mug, "Option") %><br />
<%= f.check_box(:option2) %>
<%= f.label(:mousepad, "Option2") %>
</div>
</div>
</div>

Trying to initiate a variable on page load Angularjs

I am trying to initiate a variable showName when the page loads. It worked fine when I did it using just angular but when I implemented it with rails(with embedded ruby tags) I am not able to hide the text field and save button as my variable gets undefined. I have tried couple of ways and ended with no result.
Here is my code. Any help is really appreciated. Thanks.
Edit.html.erb:
<div class="row" ng-controller="EditUserCtrl">
<div class="col-md-6 col-md-offset-3" id="editForm">
<%= form_for(#user) do |f| %>
<h1 id="editHeader">Account Information </h1>
<%= render 'shared/error_messages' %>
<div class="form-group">
<%= f.label :name, "Full Name:" %><br>
<div class="row">
<div class="col-sm-7">
<div ng-hide="showName">
<%= f.label :name, #user.name , id:"editFields" %>
</div>
<%= f.text_field :name, { :'ng-show' => 'showName'}%>
</div>
<div class="col-sm-5" ng-show="showName">
<%= f.button "Save", :action => "update_name" , :method => :put, class: "btn btn-primary", :'ng-click' => "clicked()", id:"editAccount" %>
</div>
<div class="col-sm-5" ng-hide="showName">
<i id="nav-cart-count" class="glyphicon glyphicon-edit"></i> Edit
</div>
</div>
</div>
<% end %>
</div>
Use ng-init to initialize the variable
<div class="row" ng-controller="EditUserCtrl" ng-init="showName = true">

Routing problems with STI

I've declared an STI on Works
class Work < ActiveRecord::Base
validates :title, presence: true
validates :visibility, presence: true
belongs_to :category
end
class Story<Work
end
class Poem<Work
end
Code for form:
<div class="panel panel-blue margin-bottom-40">
<div class="panel-heading">
<h3 class="panel-title"><i class="icon-tasks"></i> Item Details</h3>
</div>
<div class="panel-body">
<%= form_for(#work, :html =>{:class =>"margin-bottom-40 new_item ",:multipart => true}) do |f| %>
<% if #work.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#work.errors.count, "error") %> found </h2>
<ul>
<% #work.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="form-group">
<%= f.label "Title",:class=>"control-label" %>
<div class="controls">
<%= f.text_field :title,:class=>"form-control",:placeholder=>"Title of the story/poem" %>
<div class="description">
<div class="">
<strong>Enter a suitable title for your story/poem
</strong>
</div>
</div>
</div>
</div>
<div class="form-group">
<%= f.label :type,:class=>"control-label" %>
<div class="controls">
<%= f.select :type,{"Story"=>"Story","Poem"=>"Poem"},{},:class=>"form-control",:placeholder=>"Story/Poem" %>
<div class="description">
<div class="">
<strong>Is it a story or a poem?
</strong>
</div>
</div>
</div>
</div>
<%
visibilities=["Me Only","Anyone with link","Logged In Users","Everyone"]
%>
<div class="form-group">
<%= f.label :visibility,:class=>"control-label" %>
<div class="controls">
<% [ 3,2, 1, 0 ].each do |option| %>
<br><%= radio_button_tag :visiblity, option, #visibility == option %>
<%= label_tag "visibility_#{option}", visibilities[option].humanize %>
<% end %>
<div class="description">
<div class="">
<strong>
</strong>
</div>
</div>
</div>
</div>
<div class="form-group">
<%= f.label :category_id,:class=>"control-label" %>
<div class="controls">
<%= f.collection_select :category_id,Category.all,:id,:name,{},:class=>"form-control" %>
<div class="description">
<div class="">
<strong>
Type your story/Poem here
</strong>
</div>
</div>
</div>
</div>
<div class="form-group">
<%= f.label :content,:class=>"control-label" %>
<div class="controls">
<%= f.text_area :content,:class=>"form-control" %>
<div class="description">
<div class="">
<strong>
</strong>
</div>
</div>
</div>
</div>
<div class="actions">
<%= f.submit :class=>"btn btn-primary" %>
<%= link_to 'Back', works_path,:class=>"btn" %>
</div>
<% end %>
I have scaffolding for work with the parameter Type which accepts Story and Poem. When I submitted the form initially, I would get a stories_path not found error so I mapped stories and poems paths as follows:
resources :stories, :controller => 'works'
resources :poems, :controller => 'works'
So when I submit the first time, the form shows validation errors. When I submit it again after correcting validation, I get an exception
ActionController::ParameterMissing in WorksController#create
in this line:
params.require(:work).permit(:title, :type, :visibility, :category_id, :content)
I assume this is because its now renaming all the fields to story[] instead of work[]
How do I fix this and in the greater picture, how do I make STI work on the same controller. I just want to be able to use Poem.all and Story.all for fetching records.
Submisson currently works through the same interface
I am allowing the user to select the type of work in the form.

Resources