This is probably easy but I'm a bit of a newbie on wrapping my head around these things sometimes.
Synopsis: I'm trying to make a checklist application that technicians go through and answer questions about what has been completed or done in the field. The technicians then submit this for review. The questions are created, managed, and updated by the managers.
UPDATE
I'm a designer, so I naturally magnetize to PS. Here's a photo of the concept: http://screensnapr.com/u/a9k1ps.png
checklist model contains: header, subheader, question, and answer.
Everything is a string, except the answer field, which is an integer for a check box.
I'm not quite sure which RESTful page to start with after that though. I need the header displayed like this (in view), but editable/submittable through the check box all on one page.
This view has to DISPLAY the checklist and EDIT the checklist at the same time. The manager needs to be able to add new headers, subheaders, and questions, which the technicians can then answer.
<% #checklists.each do |checklist| %>
<h1> <%=h checklist.header %> </h1>
<h3> <%=h checklist.subheader %> </h3>
<ul>
<li>
<%=h checklist.question %>
<% form_for #checklists do |f| %>
<%= f.check_box("checklist", "answer") %>
<% end %>
</li>
</ul>
<% end %>
Would this work and would it best to stick this in the index or edit action? Would I be better doing a partial of some sort? nested_attributes? I'm a bit lost at this point because I'm trying to manage two actions (index, edit) within one file.
If you want a manager to update/modify the checkboxes and the technicians to fill in the forms, you need a couple of extra tables. One containing the questions and one containing the values that are checked. Also, it seems better to split the controller into two, one for each user type:
For the manager part you can simply make a controller like any other controller: using the index action to show all questions and the edit/update/etc actions to modify them.
For the technician part you need to define a project table, containing some information about the project the technician is working on. And a checkboxes table containing the project_id and the checkbox_ids, in order to link the checkboxes to a certain project.
See A Guide to Active Record Associations for more information about creating associations between tables.
Without looking at this further, I'm willing to bet you want
form_for checklist.question and POST to questions_controller, which would use the #update action.
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.
Hello I’m beginner on ruby on rails and I have to build an application for my studies.
It will have users who can select differents activities and then have a list of all the things that they need for these activities ( For example if they select « Fishing » they will have a list as « bundles, fishing rod … » ).
I have a bidirectional has_and_belongs_to_many relationship between activities and things.
I would like to display all the activities and if the user selects some activities it should show things related to those activities.
How can I build such a view?
I try to put an « if check_box true ? » but it doesn’t work …. If you can help me, I would be grateful !
To display my list of activities I'm using this :
<%= form_tag do %> <% #activities.each do |activity| %> <li> <%= check_box_tag 'activity_ids[]', activity.id %> <%= activity.name %> <%= activity.content %> </li> <% end %>
Since this is for an assignment I will just try to help you along but not give you the whole answer as your teacher wants you to learn it.
I would use the form_for as described in: https://apidock.com/rails/ActionView/Helpers/FormHelper/form_for
Then I would build the checkboxes using the collection_check_boxes method as shown here:
https://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/collection_check_boxes
It is unclear from your question but if the Things for each activity are already set, then you shouldn't need much more than this. If you try the methods above and have problems please edit your original question and I can try and help you further.
Try doing it via jQuery and ajax request. Create an action in a controller which retrieve data and call that action from the ajax request and then render the response data whereever you want.
Model:
Users have expenses. Expense has a status.
View:
As users add their expenses, they are shown in a list. Each expense row has a form button on the end, which is used to submit the expense (changing the status of the expense). This allows users to add expenses they have not completely filled out, and submit them when they are ready. There is no parent form on this page, just the form buttons which submit the expense to a method which changes the status, and then reloads the page.
Currently it works great, but users have asked to be able to "submit all" the expenses that are showing on the view with a single button.
Question:
What is the proper way to handle this in rails? Should I find a way to gather the array of expense id's and then submit a separate form? Is there a way to ask for a set of records present in a view with a certain status?
Thanks!
Another option, if I'm thinking about this right (big if), would be to wrap your page in a User form. Then you could have something like...
<%= form_for(#user) do |f| %>
<% #user.expenses.each do |expense| %>
<% f.fields_for expense do |e| %>
<!-- expense form -->
<% end %>
<% end >
<% end %>
This is something you could submit as a whole. I'm having trouble picturing what a single expense addition might look like, but hopefully this gets you a little further down the road.
Edit: in addition to having this User form on the page, you could have an "extra" Expense form to create an expense. When you submit a new expense, that expense appears in the list under the user form, where it can be edited or submitted, either as part of a group or individually (as part of a "group" of 1).
custom controller action:
def update_all_expense_statuses
expenses = current_user.expenses
ExpenseUpdater.new(expenses).update_expense
redirect_to :back
end
expense updater class:
class ExpenseUpdater
def initialize(expenses)
#expenses = expenses
end
def update_expense
#expenses.each do |expense|
expense.update_attributes(status: 'paid')
expense.save
end
end
end
This is just an example of one way to update all the user's expenses with a custom controller action. Just call the controller method from a link_to:
<%= link_to "Update all expenses", update_all_expense_statuses_path %>
Remember to add it to your routes. Hope this helps.
The first thing you should do is change the forms to submit remotely, ie make an ajax request. Then you're not reloading the whole page. Check out Rails' various "remote" form helpers, eg "remote_form_for".
Then, write a javascript function to submit all the forms for inputs that have changed since the page loaded. You'd probably want to add a "changed" (or similar) class to the parent form in an onchange event in each input, to facilitate this. I think this is the best way to handle the "status" thing you're asking about. Make a "Submit all" button which calls this function.
Use a form/service object http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/ to encapsulate expense report
I am new to ruby on rails and working through the Rails Tutorial book and also Rails Programming from pragmatic studio. However for my project I need to find solutions now so sad I can't spend more time on the researching.
My question is, once I am able to add, show and edit forms I am being instructed to create partials, ie _forms.html.erb and then rendering those forms on the edit, show and new pages.
On my _forms.html.erb partial, how can I implement some flow using if statements based on the page that the form is being rendered for.
For example, when _form.html.erb is being rendered for the show page I want certain form_for labels/fields to be set to readonly: true. At the bottom of the form I want submit, edit, change buttons based on the page aswell.
So far I am trying to use the parems[:action] == "new" or "edit" etc to implement the control flow as follows:
Old code under the edit.html.erb file:
<%= f.label :patform_type %>
<%= f.text_field :patform_type,autofocus: true %>
New code under the _form.html.erb file:
<%= f.label :patform_type %>
<%= f.text_field :patform_type %>
<% if params[:action] == "new" %>
<%= ,autofocus: true %>
<% end %>
My original code has been influenced by these posts:
Rails not editable text field
How to disable all form_for input fields in Ruby on Rails app?
Once I get this right then I am hoping I can use it to wrap it around other elements like the submit, edit buttons etc or other fields.
Also if someone knows a better way can you please let me know as I don't know what I don't know.
Thanks for any assistance/ideas you can provide.
You probably have to stick with a bunch of if/else statements if you need such depth of logic within the form, but I would recommend having the forms typed into their respective erb file, and not be rendered from a partial with a ton of logic.
A partial is meant for repeated code, and your code is not exactly what I would describe as repeatable. It is also not immediately understandable and will contain code that is essentially a waste of time to read:
For example, if I am reading the edit action's view and I see:
if params[:action] == "new"
It will be testing for an action that isn't even relevant to the current view, unlike logic such as:
if current_user.admin?
which will be more suitable for partial-based logic.
Hope that helps, enjoy RoR
I have two tables, Author and Book, where an author can have many books. I've got an edit view set up as "System.Web.Mvc.ViewPage(of MyDatabase.Author)". The form is set up to show the Author and all his books, with the ability to edit book information:
<% Using Html.BeginForm()%>
<%=Model.author_name%> <br/>
<% For Each item In Model.Books%>
<%=Html.CheckBox("checked_out")%>
<%=item.book_name%> <br/>
<% Next%>
<input type="submit" value="Save" />
<% End Using%>
In the controller, I've got the Post function:
<ActionName("Edit"), AcceptVerbs(HttpVerbs.Post)> _
Function Save(ByVal form As Author) As ActionResult
Dim book_count = Author.Books.Count
End Function
The problem is that the Books collection isn't part of the post - book_count is zero, even though there are several books displayed.
Am I doing something wrong, or am I expecting too much in the post? How can I make this work?
I believe your problem is that the "magic parser" for MVC has nothing to hang it's hat on. When you put items on your page that you want to be able to grab back on the post that are enumerated inside of a collection, you've got to give your "things" names. So, what I did in my page is if I didn't want them to be able to edit a field, like your book name above, I would use a hidden field to wrap the value up in a control that MVC can get at via magic and then also display that value to the user. So, it could look something like:
<% for (i = 0; i < Model.Books.Count; i++) {
book = Model.Books[i] as book //I'm a C# guy so make this VB
<%= Html.CheckBox("author["+1+"].checked_out", book.checked_out) %>
<%= HtmlHidden("author["+i+"].book_name",book.book_name) %>
<%= book.book_name %>
<% } %>
...and then this should come back all nicely packaged for you as a book collection in your Authors object. See if that gets you in the right direction.
EDIT
One other thought too. I'm using the UpdateModel method to fetch the results. It shouldn't matter, but I thought I'd throw that in there.
See here for the full answer to this problem. I basically didn't grok how MVC wanted the variables to be named.