I have a form that creates a new PlanEntry.
At the top of this form I have this link to upload a video as part of the PlanEntry:
<%= link_to "Upload Video", new_video_sources_path %>
This takes a user to another form to upload the video. When successful, it returns back to the new PlanEntry form.
This works fine if the user always starts by uploading a video, but in some instances they may already draft things like a 'title' and 'description'. In these cases, these values are lost when the user returns to the form after uploading the video.
How can I preserve these values?
I figured it out, but in a bit of a roundabout way. Basically I replaced the line above with this:
<%= f.submit "Upload Video", :name=>"commit", :value=>"Upload Video" %>
This is a second button to submit the PlanEntry form. Now in my controller I check to see which button was clicked in the submission. If it was the "Upload Video" button, I save all the parameters to the session, and redirect to the upload video form.
if params[:commit] == "Upload Video"
session[:plan_entry] = params[:plan_entry]
redirect_to new_video_sources_path
else
...
end
When the video is uploaded it redirects back to the new PlanEntry form. In my PlanEntry#new controller I check if there are existing session variables. If so, I grab them and then clear them.
if session[:plan_entry].present?
#plan_entry.assign_attributes(session[:plan_entry])
session[:plan_entry] = nil
end
Related
I have a page where a deactivated user account can be activated - but before they can be activated, they need to be assigned a new password. Currently I have two buttons to perform these actions:
At the end of the 'add password' form there is this button:
<%= f.submit %>
A little further on the page there is this button, which sets the status of the user to 'active':
<%= standard_button 'Activate user', update_status_user_path(#user),
:method => :patch %>
If possible, I'd like add the functionality of the second button to the submit button.
I don't fully grasp the magic behind forms and their buttons, so I am wondering whether this is possible and if so, how I could accomplish this?
You can think of a button as a mini form. View the docs to get more information about the button_to rails method.
In your form_for, there will be a url which specifies where the form is posting to. In that controller method, you can include the logic to update the user's status to 'active'. Something like
def some_method
user = User.find(params[:user_id])
user.update_attribute(:status, "active")
end
I have a form with nested attributes for attachments.
Everything seems to be working fine till the moment user submits the form with error on a field other than the attachments.
At the first submit of the form I re-render the form displaying the errors but the nested fields for the files he/she attached don't display their values, they are empty. Thus, when user fixes his/her error, the form submits with no attachments.
How should I handle this; I want when rendering the form with errors to have the file inputs with the previously submitted values. If you suggest another approach be my guest.
Sample code:
= simple_nested_form_for foo do |f|
= f.input :name
= f.fields_for :attachments do |af|
= af.input :attached_file
= f.submit 'Create'
if you want to send image of file through ajax then u cant send it directly as ajax is used to send data to server without refreshing the current page it accepts less amount of data.
Other way to send file through ajax is to either hack or else the simple one use remotipart gem remotipart
I have a form where I create a model and I want a dialoge box to appear if the user navigates away from the page, unless they click the save/create button.
I have this javascript code that works anytime the user leaves the page; meaning this dialoge still appears when the user clicks save/create.
#javascripts/workouts.js.coffee
window.confirmExit = () ->
"Your changes will not be saved.";
#workouts/new.html.haml
= render 'form'
:javascript
window.onbeforeunload = confirmExit
#workouts_form.html.haml
= simple_form_for(#workout) do |f|
# some input fields
= f.button :submit
Now I know that I only want the confirm exit called if the submit button is not clicked, but I am not sure how to go about doing this.
What I usually do is I only show the confirmation when changes actually have been made to the form. To do that, you'll need to scan the forms on your page, serialize them and store it in memory. Then, when the user leaves the page, serialize the form again and see if there is a difference.
Otherwise, you might want to bind some function to submit event of the form, setting some global boolean like window.formSubmitted to true. Check that variable when leaving the page to determine if you want to show the confirmation box.
So I figured it out, at least it works for chrome and safari although right now I am under the impression that it will not work for ie. What I did was create a boolean that is set to false and only set to true when the submit button is selected.
#javascripts/workouts.js.coffee
window.submitButtonClicked = false
window.confirmExit = () ->
if window.submitButtonClicked
null
else
"Your changes will not be saved."
$(document).ready ->
$('#target').submit ->
window.submitButtonClicked = true
#workouts/new.html.haml
:javascript
window.onbeforeunload = confirmExit
#workouts_form.html.haml
#target
= simple_form_for(#workout) do |f|
# some input fields
= f.button :submit
I hope this helps anyone who was curious. Note that the #target line is actual haml and not a comment.
I'm working on a small picture application. That I'm trying to do is build a counter to track how many times each image is clicked.
Right now I have in my view:
<% #galleries.each do |g| %>
<% for image in g.images %>
<div id="picture">
<%= render 'top_nav'%>
<%= link_to g.source, :target => true do %>
<%= image_tag image.file_url(:preview) %>
<% g.vote %>
<% end %>
<%= will_paginate(#galleries, :next_label => "Forward", :previous_label => "Previous") %>
</div>
Obviously this doesn't work, as the g.vote executes every time it's rendered, not clicked. Here's the vote method in my model:
def vote
self.increment!(:score)
end
I'm looking for a solution to run the vote method only when the image above is clicked. The links are to external resources only, not to a show action. Should I be building a controller action that's accepts a post, executes the vote, then redirects to the source?
Anyway, looking for some ideas, thanks.
I've done something similar, but keeping a count of how many times a Download link was clicked. This was awhile ago and I didn't know about Ajax at the time, but now I would recommend using jQuery (a great library in my opinion, but you could use something else) and do an Ajax call when the image is clicked that would execute some controller action which would increment that vote.
The other way, which is what I did in my scenario, and is what you talked about there, is creating a custom action in the controller that accepts a post. But I have to ask as well, does clicking on the image do something else in the behaviour of your website? For example, if when you click the picture, another random image is supposed to come up, that means you'll already have an action to load a new image and it be easy to stick the vote up in there before showing a new image. Otherwise you'd have to create the new controller action. If that's the case, the Ajax would be more efficient as the user wouldn't see a momentary flash as the page was refreshed (especially bad if the refresh time is long).
I am trying to have a way of confirming the information entered before actually saving it to the DB
Considered making an individual confirmation page as discussed here
Ruby on Rails: Confirmation Page for ActiveRecord Object Creation
However my form includes an attached file using paperclip which makes it more of a hassle to implement.
I thought of just having a :confirm => pop up that would show the information that the user
had just entered.
The problem is how to show the information that the user had just entered, for example
<% form_for #entry, :html => { :multipart => true } do |f| %>
<%= f.error_messages %>
<%= f.label :name %><br />
<%= f.text_field :name %>
<%= f.label :file %><br />
<%= f.file_field :file %>
<%= f.submit 'Create', :confirm => "????? " %>
<% end %>
Given that your loading attachments it may not be a bad idea to render a staging view including information derived from the attachment allowing the user to confirm. As in display the file if it's an image, or the first paragraph of text if it's a text file, etc.
It's going to take more work than the just adding a confirm pop up, but I feel the enhanced user experience is worth the extra effort.
I'm not familiar with the way that paperclip works. So you're mostly on your own for the intimate details.
You will probably have to create a record before the staging view can be rendered with the sample of the uploaded file. To accomplish that I'd set up an "active" column on the model in question, that defaults to false.
Usage would look something like this:
User complets new form.
Attachment is updated and records are saved, with the active field set to false.
Redirected to confirmation page that is essentially the show page with a confirm link/button and a cancel link/button
a. When the confirm link/button is clicked it sends a request to the controller triggering the update action on this record setting active to true.
b. When the cancel link/button is clicked it sends a request to the controller trigering the destroy action on this record.
All that's left is to set up a recurring task to remove objects that are inactive and were crated long enough ago that it's safe to assume the user has just ended the browser session.
The confirm option for the Rails submit method can only take a text value.
If you want to dynamically generate the confirm text, one way you could do it is to write your own HTML submit tag, and write custom javascript to analyse the fields you want to use in your generated text.
Or you could use the Rails submit method, but use something like JQuery to add an event handler to it.
I'd just throw my js into an onclick handler. That's all Rails does.
<%= f.submit 'Create', :onclick => "confirm('Name is ' + $F('entry_name'));" %>
(note, didn't test that, but it looks close. confirm is a core js function, not part of any lib)