So, I am using a form_tag to create my own search facility on a site displaying items.
I have a collection_select which lists locations of the items. I initially used prompt: 'n/a' to display 'n/a' without adding 'n/a' into the list of locations and then tried to use select: params[:search][:location] to keep the selection in the list after submitting. Basically I could get one of them to work at a time. Never both. I have written a work-around below:
<%= collection_select(:search, :location, Item::LOCATIONS, :to_s, :titleize, prompt: (#location ? params[:search][:location] : 'n/a')) %>
This is working but it feels wrong. I have obviously created a variable called #location in the controller for when the params exist and then added logic into the collection_select tag.
Any cleaner solutions to this would be appreciated. I am very new to Rails (and coding!) so trying to learn best practices.
Related
I'd like to have a drop down in my Rails form where users can select an area of a city, e.g. "Marchmont", "New Town", "Baberton" etc, when adding an order. I'd like that once they have made a selection, this will then be the default selection for the following times they use the form to add an order (so that they don't have to keep selecting it) but also that they can change their selection at any time. Hope that makes sense. I'm using the Simple Form gem. Thanks in advance! :)
#Steve
I will make a couple of assumptions.
1.) you know how to create forms within the rails templating engine.
2.) you understand how to create a dropdown menu using the Simple Form gem
So you have a couple of options based on what you actually want to accomplish. Based on what you are briefly describing, it sounds like you have some kind of an e-commerce/checkout situation that you want auto-completion to make it easier for a user.
there are a couple of approaches to storing this data.
Saving the user Data.
1.) Save it right on the user model under district_of_last_order
2.) Save it right on the order model that a user has_many orders. Then you can pull the first order's city district and select that
Personally I would lean on #2 as you probably want to be able to tightly couple the order with the user and saving that information twice is redundant since you can always do something like current_user.orders.first.district or whatever,
in your ERB where you build the form you can then do something along these lines:
<%= simple_form_for(#order) do |f| %>
... other input fields
<% if current_user.orders.first %>
<%= f.input as: :select selected: current_user.orders.first.district %>
<% else %>
<%= ... your regular dropdown menu here without the default %>
<% end %>
... rest of your form
If you have the option of using gems, I have had good results with select2-rails.
I am new to activeadmin / formtastic and I have having a bit of trouble understanding how things work. I read through the documentation on how to create a form using formtastic but I seem to be still running into issues and I am sure its me not understanding how things work.
I am creating a discussions application very similar to a blog application and the end result is that I would like to create an interface for the administrators to add comments to discussions without having to go into the users interface.
My starting point is the discussions view in the admin section presented by activeadmin. I am attempting to work on the add comment form. According to the instructions, I should be able to add a form using
form partial: 'new_admin_comment_form', locals {discussion_comment: DiscussionComment.new}
which then I should create this partial in app/views/admin/discussions folder. I have done that and have entered some arbitrary text to make sure the partial renders and it does. But once I start adding code I am not able to get the form to display.
The current code I am working with is:
<%= semantic_form_for [:admin, discussion_comment] do |f| %>
<%= f.inputs, :body %>
<%= f.actions %>
<% end %>
So a few questions I have that I wasn't able to find in the documentation:
Where do I create instance variables to be used in my form? I have been setting these in the activeadmin files and that is bothering me.
How do I pass params around? I assumed I could do this as normal yet when I try to view them using <%= debug params.inspect %>, it is empty even when I should have at least the id that was in the parent form. Even when using locals: {id: params[:id]}, id is empty in the partial.
What are the best ways to debug why my form is not appearing? Am I able to use regular ERB if worse comes to worse?
You can do this without a custom form. If you stick to the active admin DSL you can use its has_many method. Example here:
http://www.activeadmin.info/docs/5-forms.html
Your Discussion model should look like this
class Discussion < ActiveRecord::Base
has_many :discussion_comments
accepts_nested_attributes_for :discussion_comments, allow_destroy: true
end
Building a form for users to submit data. I can't seem to understand or figure out how *collection_select* method works.
Essentially I'm trying to give my users the option to choose which sub_category their product belongs in when submitting the form. What should the collection_select syntax look like?
I ended up figuring it out based on comments, here is what I ended up with, in case it helps anyone else.
<%= f.collection_select(:sub_category_id , SubCategory.find(:all), :id , :name) %>
Edited
SubCategory.all
instead of
Sub_category.all
I have a controller named store_controller.rb and method index that renders index.html.erb, displaying some products. I'm thinking about the chance of placing in this view a form.select (a sort of drop-down menu) with values like (high price items, economic items). I want these values select products by a value returned by an helper method, that in call in view index.html.erb.
My question is: Developing this drop-down menu - using, for example, f.select - is it possible to link a drop-down menu in Rails to a value returned by an helper method? For example: <%= f.select :product, :returned_value_by_helper_method [[title, value..]] %> and so on?
Try select_tag, to which you can add whatever options you like (search web for options_for_select and options_for_select_tag). This will be static, however. If you want dynamic, consider Unobtrusive JavaScript and form observers.
I use formtastic to create new and edit forms for my resources.
with something like f.inputs it fetches and displays all my fields automatically.
I would like to know if there is something similar that makes life easier to write the index and show views. I tried using formtastic with 'disable' to show a readonly form but that's not only undesirable but also makes like messy for radio button/check box inputs where I want a summary instead of all the details.
I'm the creator/maintainer of Formtastic. I too have use the "disabled" hack to get "show" views nice and quick. It's not ideal, but it does work. This doesn't specifically answer your question, but in regards to the radio buttons, you can always do :as => :string for the "show" view. I'm doing this lots actually!
I started Attrtastic project at my previous work and I now rewrite it from scratch (while refactoring a bit). I hope to get all I already done in previous version (nested objects collections, space for buttons/links, stuff like 'edit','delete','new') to the end of the month.
Viewtastic attempts to do that. Although I haven't personally used it.
UberKit is another one I found (although I haven't used it either... hoping to on a new project).
For the index I would definitely recommend checking out thoughtbot's sortable_table. It makes it super easy to creat clickable sortable tables. Combine with will_paginate. Add inherited_resources to get rid of most of your controller code. And you're set!
I use simple_form - https://github.com/plataformatec/simple_form. Easy to use.
A quick way to display all accessible attributes:
Replace Album with your model
<%- model_class = Album -%>
<% model_class.accessible_attributes.select { |a| a != "" && !a.nil? }.each do |a| %>
<b><%= model_class.human_attribute_name(a) %></b> : <%= #album.send(a) %><br/>
<% end %>
If you want to filter out some attributes, define an array of attributes you would like to display in your controller, pass it to the view, loop through the array just like we did above (replace model_class.accessible_attributes by #attributes_to_show).
Please note that accessible_attributes on an AR class is only available in Rails 3.2.2 and later.