Is there a way to pass an id via checkbox?
Let's say we have Model Record which stores information. Any user who want to edit a record will need to propose it first for the Admin to approve. The problem is Record is linked to multiple Image model (with paperclip attachment) so each Record has_many images. The problem is that I am stuck with how to allow users to propose a deletion of existing photos. I imagine it would be great to store a list of proposed deletion, with image_id and proposal_id to store the deletion waiting for approval but I can't find out a way to program the checkbox.
The checkbox would need to pass in the image_id marked to delete. Is it possible and if so how can I do that? Thanks!
<% #record.images.each do |image| %>
<%= check_box_tag "image_ids[]", image.id %>
<%= image_tag image.url %>
<% end %>
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 not sure what the correct approach is for my situation:
I want to create a link_to pushing all checkboxes with value="1" into an array, or individually if array is not possible, but I am at a loss of how to express that?
<% #cards.each do |card| %>
<%= check_box("#{card.name}", card.id, {checked: true}) %><%= "#{card.name}" %>
<% end %>
(Rails 4.2)
After a long time of dead ends, trying to make it a 'clean' solution I ended up with this very dirty approach. But as they say, done is better than perfect:
Create a link_to that would include all the cards, but add one additional params: user_selected_cards = "".
Create a javascript that listens to for checking/unchecking of the checkboxes and reads the id associated with that specific checkbox. Then take that incoming info and add or remove it to the actual url that the link_to generates by finding the user_seletected_cards= portion in the url and add or remove the id depending on if isn't or already is added to the list after the equal sign.
Context:
I have a forum thread model which acts_as_votable.
The up vote functionality works fine
The up vote button allows the current_user to up vote the forum thread only once, which is the desired functionality. I am trying to change the css of the up vote button to a different color once the current_user had clicked on it.
<%= link_to forum_thread do %>
<h3>
<%= link_to like_forum_thread_path(forum_thread), method: :put, class: "" do %>
<span class="glyphicon glyphicon-arrow-up"></span>
<% end %>
<%= forum_thread.get_upvotes.size %>
<%= forum_thread.subject %>
</h3>
<% end %>
Problem:
One way to do this is to get a list of all the user_id's(using #forum_thread.votes_for_ids) who have up voted the forum thread and check if the current_user user id matches the list. If so, then disable the button. Is there a more efficient way to do this ?
Solution first:
#user.voted_for? #post
Its available in the documentation as well. Here
Now, lets go to the details:
You current solution does the following:
Fetch all the votes on the Post. Ideally, see to it that you get MySQL to return as fewer ActiveRecord objects as possible. Because, there is a considerable performance loss converting each MySQL row into an Active Record.
Looping through all the ActiveRecords and collecting the USER_ID in it. Looping is another performance hindrance, if you could easily avoid it.
Instead, prefer a MySQL query that returns the ROW/Data of what exactly we need, "If the current user had voted on the Post". something like (You would need to use the correct/apt database table names):
PostVote.find_by_post_id_and_user_id(post_id, user_id).
The above returns if the user had voted or not.
Alternatively, Acts_As_Votable does provide the same without you having to do the heavy lifting:
#user.voted_for? #post
The App:
My application has many buses. Each bus has many photos of the interior and exterior. Some of the interior photos have an associated image that indicates the location on the bus where the photos were taken (like a "You are Here" marker on a map), which is assigned through :parent_id attribute that corresponds to the "parent" photos id.
The Goal:
Output the URL for the "child" image if a selected photo is a "parent," and keep blank if not.
The Problem:
The only way I know how to find any particular associated image brings back an array. Unfortunately for me, the photo.url method that comes with Paperclip can't work with an array.
This is closest to what I want, but, again, it brings back an array that I can't use to find the image URL.
def assigned_floorplan(where I pass in all the parent images as a params)
bus_images.all(conditions: { is_floorplan: true, parent_id: params.id })
end
Is this a dead end, or is there a way to pull out the id of the associated image from the array so that I can use the photo.url method? Or am I going about this the wrong way? I'm willing to approach this problem totally differently if you have any suggestions.
You have to call the photo.url method for each image separately. A nested loop would work:
<% #parent_images.each do |parent_image| %>
<%= parent_image.photo.url %>
<% parent_image.child_images.each do |child_image| %>
<%= child_image.photo.url %>
<% end %>
<% end %>
I've got an application where the user can set up a folder to keep notes in. What I had previously was a hidden form field to store the id of the person who created it, i.e.:
<%=f.hidden_field 'user_id', :value => current_user.id %>
However, I now need to add a 'keyholder', who has read-only access to this folder. I have a list of links, which only appear if the user has added a folder, or the keyholder can set one up for them.
The keyholder is a regular user themselves, so the above code would only set up a folder with their own id, not that of the person whose account they are accessing. The keyholder has an 'access_id' that matches the user id of the the person whose account they can access.
How do I set it up so that the form is capturing the right user id?
What I'm trying to acheive is the following (this doesn't work, but might give a better idea of what I mean):
<% if current_user.access.folder.nil? %>
<li><%= link_to 'Create a Folder',
new_folder_path(:user_id => current_user.access_id) %></li>
<% end %>
And what would I need to change in the folder form partial to get it to accept this user id? Thanks!
You'd better use an authorization gem such like cancan
I'm not sure i completely understand what you're trying to do, but rather than storing the user id in a hidden field. Just use <%= current_user.id %> on any page as it seems your doing.
Then depending on how your models are setup, just create a helper method to check access of the page that the user is on. I'm assuming your helper will check the params[:id] or params[:user_id] to get the current page.