Just trying to save the values of 4 checkboxes into a column named reminders to my Tickets Model.
But, either the values aren't being saved, or when I go back to edit this "Ticket" its not ale to pull the data from the db and display the true / false vaules correctly.
Any advice?
/tickets/_form.html.erb
<%= form_for(#ticket) do |f| %>
<div>
<% [ 'S.T.A.R.T', 'E.N.D.E.D', 'URGENT' , 'Repeat Request' ].each do |reminder| %>
<br><%= check_box_tag 'reminders', reminder, (params[:reminders] || {}).include (reminder) %>
<%= reminder.humanize %>
<% end %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Rather than using check_box_tag the way you are, change it to use your form builder object provided by form_for. It'll ensure that the name of the checkbox is correct which is your problem (I think).
Because you're not using it, the checkbox is probably taking on a different name to what rails is expecting to see, so when you submit the form, the param name won't be associated with your record and thus won't be changed. Try using
<%= f.check_box_tag :reminder %>
Rails will automatically handle whether or not it is ticked when the page loads :)
Related
I am building a basic bare bones social media app right now.
I have a user class and a status class.
For each status, there is a "creater" (a user object) and a "subject" (a user object that the status is about). I was able to create tags by using the acts_as_taggable_on gem. What ends up happening is when a user goes to create a post, he/she can select another user from a dropdown menu. The chosen user's id attribute is then stored.
Now I am trying to link to the chosen User's profile. This is my code for show statuses on a profile page.
<% if #statuses %>
<% #statuses.each do |status| %>
<div class="well">
<%= status.content %>
<br></br>
#link to user who's associated with the tagId
<%= link_to User.find(status.tag_list).profile_name, user_profile_path(User.find(status.tag_list).profile_name) %>
<hr />
<%= link_to time_ago_in_words(status.created_at), status_path(status) %> ago
</div>
<% end %>
<% end%>
this is the line where the above code breaks
<%= link_to User.find(status.tag_list).profile_name, user_profile_path(User.find(status.tag_list).profile_name) %>
Can anyone help me out with this?
Not surprised this line is failing:
<%= link_to User.find(status.tag_list).profile_name, user_profile_path(User.find(status.tag_list).profile_name) %>
A couple points:
It's a little cleaner to separate it onto multiple lines
I suspect your problem is because you're passing a profile_name to user_profile_path instead of an id, though I can't be certain without seeing your routes.
Try the following:
<% profile_user = User.find(status.tag_list) %>
<%= link_to profile_user.profile_name, user_profile_path(profile_user.id) %>
I'm sure I'm missing something simple, but I'm generating a list of projects each with a form to add a new audition to that particular project. The form isn't being generated properly.
The view:
<% #projects.group_by(&:aasm_state).each do |state, projects| %>
<div class="project-column small-order-<%= state_priority(state) %>">
<div class="project-state spaced-out-2 project-<%= state %>"><%= state.titleize %></div>
<% for project in projects %>
<%= render partial: 'auditions/partials/dash_project_audition_form', locals: {project: project} %>
<% end %>
<% end %>
This is the code in the partial:
<button data-click="new-project-audition">+ Audition for <%= project.title %></button>
<%= form_for([project, #audition]) do |f| %>
... SNIP....
<% end %>
Everything works except the form. Each form generated contains the ID for the last record in #projects. So, for example the last project is ID 19, no matter which project form I click on, they're all for 19. The problem isn't with the partial, it's with something I'm doing in form_for, because + Audition for <%= project.title %>, which is inside the partial, generates the correct title.
UPDATE:
I restarted the server and form_for is now generating the right path, with the right ID.
I have this line also inside the form_for loop, and it's still wrong.
<h3 class="section-head">New Audition for <%= project.title %></h3>
It's generating the title for the last project in the whole loop.
UPDATE 2: Stopped working on a page reload, so... not even sure what to say now.
when you do form_for([project, #audition]), it generates a form for
projects:project_id\auditions:audition_id
so the id that you see in all forms may belong to the #audition variable. Where do you initialize it ?
It should be initialized ideally on a per project basis, right ? Let me know if this helps in any way.
I have a form that has fields with same name because of the "flow" of the form.
If the member is Undergrad:
<div id="if_undergrad">
<%= f.fields_for :academic do |academic_full_degree| %>
<%= academic_full_degree.text_field :major %>
<% end %>
</div>
But, if the member is Alumni:
<div id="if_alumni">
<%= f.fields_for :academic do |alumni| %>
<%= alumni.text_field :major %>
<% end %>
</div>
And I have a jQuery to show each div if the user selects alumni/undergrad from a drop-down.
If the member selects that he is Undergrad, Rails won't save the major into the database (I assume is because the major field of Alumni is blank).
Do you know how to make it work with the same name of fields?
Any help will be appreciated. Thank you!
You can disable the fields that you don't want submit then they will not send to the backend.
Somenthing like that:
$("#if_alumni input[name*='major']").prop('disabled', true);
My code:
<% #pg.items.each do |i|%>
<%= i.item_name %> # will display item name
<%= i.price_group_lines.where(price_group_id: #pg.id).take %>
<% end %>
In browser I see #PriceGroupLine:0x007fcc05e53598.
How do i get a value?
Here you are trying to display an entire row from the db:
<%= i.price_group_lines.where(price_group_id: #pg.id).take %>
But you probably want to show a value from the row, like:
<%= i.price_group_lines.where(price_group_id: #pg.id).take.amount %>
That will show the amount (if that attribute exists) from the price_group_line.
I have a code where a user can select a specific file to be deleted or analyzed.
<% if #files%>
<%= form_tag what_to_do_files_path, method: :get do %>
<%= submit_tag "Delete selected", :name => 'delete' %>
<%= submit_tag "Analyse", :name => 'analyse' %>
<% #files.each do |file| %>
<% if (file.analyzed=="no") %>
<p><td> <%= check_box_tag "files[]", file.id %></td><%= file.name %></p>
<% else %>
<div class="my_profile_info">
<p><td> <%= check_box_tag "files[]", file.id %></td> <%= file.name %></p>
<td class="Info">
Info
</td>
</div>
<% end %>
<%end%>
<%end%>
<%else%>
<%end%>
I need to be able to give a name to every analysis.
For example: user selects 3 files, enters a name in the text field "Analysis of annual profit" and clicks on the button "Analyse".
The name "Analysis of the annual profit" and the names of the files that were selected have to be saved into the table group_analysis.
I have tried something like this after submit_tag "Analyse":
<%= form_for #groupanalysis do |f| %>
<div class="field_label">
<%= f.label :group_name, "Type group name hier" %>
</div>
<br class="clear" />
<br />
<% end %>
but it tells me undefined method model name
Thanks in advance.
I think you may need to take a step back and think of how this form represents the model that you're trying to create or update. Generally speaking the first argument to form_for and form_tag is an object and symbol, respectively, which represent the model that you're working with. The form fields map to each attribute of the object.
According to conventions and/or the :url argument, this will get routed to the appropriate controller and call an action according to the HTTP verb (again, part of many conventions in rails).
Going back to your code examples, you are using the form_tag helper incorrectly and the example using form_for may not be the right implementation. For example you're just displaying a label, with no input nor submit.
I hate to just post a link here and just tell you to read the docs, but in this case I think this is the best first step.
http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-form_for