Multi select file upload field with nested attributes in Rails - ruby-on-rails-4

Currently I have a Note model which accepts nested attributes for an Attachments model, which uses Carrierwave. When adding a Note, I have a nested form to allow attaching file to the new Note:
Nested form field:
<%= f.file_field :image, multiple: true, name: "attachment[file]" %>
I am using the Cocoon gem to add the nested field. While I can easily let them add multiple file upload fields with Cocoon, and add multiple attachments that way, I only want to load one file upload field, and let them use multi select to select multiple images.
When I do this, the file upload field says '2 Images' next to it. However, upon form submission, only one file is listed under 'attachments_attributes'. I need all of the attachments to submit at once as the Note has not yet been saved.
What is the proper way to accomplish this? I am aware of the Railscast on this topic however it did not seem to address my particular scenario.
Any help is appreciated.

Just append [] to your params
<%= f.file_field :image, multiple: true, name: "attachment[file][]" %>

Related

Getting same style in another page when click to login

I am having a login page for user generated by devise gem.where I removed the registerable from model for only login not for signup.I have also generated an admin which gives default login credentials for user in seed.rb file.I have done some css work in login page. next I have generated an employee page for further process for users.
Here my doubt is the styling part what I have done in login page is also coming in employee page. I dont want that so I wrote some condition in application.html.erb
<% unless user_signed_in? %>
<%= render 'layouts/heading' %>
<% end %>
The method is defined in application.controller.rb
def user_signed_in? %>
render :layouts => 'application'
end
I tried a lot by changing conditions but it is still displaying or showing errors.
You said that you're using "device gem", but since you're talking about authentication, I assume you mean "devise"?
The user_signed_in? method is a helper which should be provided automatically by devise, and you should be able to use it in your views. You shouldn't need to define it in the application controller.
You probably want to make sure you do "before_filter :authenticate_user!" in the relevant controller (you can use the ":only" option if you only want this to apply for certain methods).
It's difficult to give any more information because you haven't said what errors you are getting. I assume you're getting some kind of syntax error on the definition of the "user_signed_in?" method, since you have an invalid "%>"on the end of the line, which I guess you copied-and-pasted from an ERB file.

Rails 4: tell simple_form to use input type "date" instead of showing different select boxes like Rails 4's f.date_field?

When I use Simple Form it shows three different select boxes for column type date. Instead of this I want Simple Form to show date attributes as HTML5 does. Specifically I want Simple Form to do the following:
<input type="date" name="org[established_at]" id="org_established_at">
For this I tried:
<%= f.input :established_at, as: :date %>
But this produces three different select boxes for date picking.
How do I tell Simple Form and Rails 4 to use input type "date"?
Recently I've done it like this:
f.input :established_at, as: :string, input_html: { class: :datepicker }
and used http://xdsoft.net/jqplugins/datetimepicker/ to show the calendar
In my project I used next:
= f.input_field :year, as: :datepicker, class: "string form-control teacher_kpk_year", readonly: true
Gemfile:
gem 'bootstrap-datepicker-rails'
applications.js
//= require bootstrap-datepicker
If you have a field defined as date, you only need add the parameter html5 like this:
f.input :established_at, html5: true
But, if you haven't defined your field like date or something like that, you only need to add the alias to the previous declaration, like this form:
f.input :established_at, as: :date, html5: true
And both forms will render a date or datetime picker like bootstrap-datepicker
best regards,

How to auto set a form value on the next page using link_to? (Rails 4)

I am new to rails and am looking to do something very simple but don't know the correct syntax. I have 2 buttons one for creating a new page and one for creating a new post. Both a page and a post save in the same database table and are controlled through a boolean field called 'static'. A page therefore has a static value of 1 and a post 0. What I want to do is simply auto set this value in a form (and hide it) when I click new page or post. I imagined the link to create a new page would work something like this:
<%= link_to 'New Page', new_page_path(:static => "1") %>
This doesn't work so I tried to create a new_static page action and a new_post page action with correcting routing (for displaying only pages I created a show_static action used the following link_to and it works fine):
<%= link_to "Pages", show_static_pages_path(#pages), :method => :get %>
The problem is when I created the new_static page action it expects an id for some reason.
new_static_page GET /pages/:id/new_static(.:format) pages#new_static
I would prefer to not mess around with new actions and routing and simply set the value with link_to. Any tips would be greatly appreciated.

dots in rails URL

I'm trying to add a show page to my devise users but can't seem to get the id value passed properly, how do I create a link to the show page? For some reason rails is doing this to URL
/show.1
users controller:
def show
#user = User.find(params[:id])
end
user/show.html.erb
<h1><%= #user.username %></h1>
link to user profile also gives me issues id no method
<%= link_to #post.email, users_show_path(#user.id) %>
routes.rb
get "users/show"
get "pages/faq"
get "pages/about"
devise_for :users
resources :posts
You have defined the show route as:
get "users/show"
Notice that there is no dynamic segment in this route, like :id. That means your route is not expecting any value to be passed. BUT while linking to this route you passed #user.id
<%= link_to #post.email, users_show_path(#user.id) %>
which the route was not expecting. So, Rails assumed that you are passing the format (like .html, .json, etc.) which is why you see the url formed as users/show.1.
To fix this, I would suggest you to add a dynamic segment to your route to capture the id of a user correctly.
Change
get "users/show"
With
get "users/show/:id" => "users#show", as: :users_show
For a user with id = 1, when you click on the user profile link the url generated would be http://yourdomain/users/show/1

rails3-jquery-autocomplete foreign key

Hi I need help with setting up the rails3-jquery-autocomplete gem with a manually assigned foreign key in my database.
Here is what my models look like
class User < ActiveRecord::Base
has_many :reservations, :foreign_key => 'reserver_id'
attr_accessible :login, :first_name, :last_name
end
class Reservation < ActiveRecord::Base
belongs_to :reserver, :class_name => 'User'
attr_accessible :reserver, :reserver_id, :reserver_login
end
The users table in my database has all of the columns
The Reservations Controller has
autocomplete :users, :login
Routes.rb has
resources :reservations do
get :autocomplete_users_login, :on => :collection
end
And in the reservations view I have this
<%= f.autocomplete_field :reserver_id, autocomplete_users_login_reservations_path %>
Now when I try and test it I see that the calls are being made in my javascript console but I get the error 500. For example I tried searching for xno which is in a login column of my users database.
GET http://0.0.0.0:3000/reservations/autocomplete_users_login?term=xno 500 (Internal Server Error) jquery.js:8241
jQuery.ajaxTransport.send jquery.js:8241
jQuery.extend.ajax jquery.js:7720
jQuery.each.jQuery.(anonymous function) jquery.js:7246
jQuery.extend.getJSON jquery.js:7263
a.railsAutocomplete.fn.extend.init.a.autocomplete.source autocomplete-rails.js:17
$.widget._search jquery-ui.js:6547
$.widget.search jquery-ui.js:6540
(anonymous function) jquery-ui.js:6335
Does this have anything to do with the fact that I am using foreign key in my database? If yes how should I structure my routing. I was following the gem documentation and tried to set it up so that I can look up the users table and list people by the login column of the users table, but so that the id gets returned and stored as :reserver_id once the user is selected. Previous code that worked with select field was
<%= f.select :reserver_id, User.select_options, :prompt => true %>
where select_options method creates an array of login strings and id pairs in individual arrays.
I know that the solution is probably really easy, thank you for your help...
My problem was silly but it turned out it had nothing to do with the fact that the category table used a custom foreign key. My first problem was using plural version of the table name rather than the singular. Once I changed all references to users to user I noticed that javascript console wasn't throwing any exceptions at me so I figured that I had either broken everything and the call wasn't being made or it was going through and it wasn't rendering properly.
The presence of proper MYSQL calls in the console confirmed my suspicion. That's when I saw that the list was rendering way off to the right of my page probably due to strange styling happening somewhere else.
Bottom line is having a custom foreign key doesn't change usage of the gem at all. THe original table name should be used.