I have a form for a new user where I want the user to pick the avatar. I've followed a few tutorials, and it all works, but UI/UX is bad as you first have to submit the complete user form before the image will be shown. Is there a way to show an image while the user is entering the data for the new account?
To simplify things, lets assume this is my form
<%= form_with(model: user) do |f| %>
<div id="avatar_picker">
<%= f.label :avatar%>
<%= f.file_field :avatar %>
<%= link_to "Delte avatar", purge_avatar_user_path(current_user), method: :delete %>
</div>
<div>
<%= f.label :email %>
<%= f.email_field :email %>
</div>
<%= f.submit "create" %>
<% end %>
Now what I want is that when a user picks an avatar, it's displayed in the avatar_picker div immediately so that the user can see what it gonna look like.
Right now I have to submit the form in order to see the picture.
Any good practice advice? thanks!
Related
I have set everything up for active storage and when I add a trip with an image, the app redirects to that trips show page and I can see the image. When I lave and go back to that show page I get the error
`Can't resolve image into URL: undefined method `persisted?' for nil:NilClass`
Here is the show page for trip
<h1> Location: <%= #trip.location %> </h1>
<h2> Date Visited: <%= #trip.date %> </h2>
<%= image_tag #trip.cover_photo, :style => "max-height:300px"%>
<h2> More Photos </h2>
<%= link_to "Add a photo", new_photo_path(#trip) %> <br>
<%= link_to "Public Profile", trips_path(#user) %> <br>
Here is the new trip form
<h1>Add a new trip </h1>
<%= form_for #trip do |f| %>
<%= f.label :location %>
<%= f.text_field :location %> <br>
<%= f.label :date %>
<%= f.text_field :date %> <br>
<%= f.label :cover_photo %>
<%= f.file_field :cover_photo %> <br>
<%= f.hidden_field :user_id, :value => session[:user_id] %>
<%= f.submit "Submit" %>
<% end %>
I'm not able to see the error since the photo uploads and displays correctly at first.
Had the same issue, except that I wasn't able to display the images even once. In your case, seems that the first time you are watching a temporal image, which is deleted after you live that view.
I realize that I was forgetting to add the attachment to permitted parameters in my controller, so the image wasn't saving. Your params should look like this:
def trip_params
params.require(:trip).permit(:location, :date, :cover_photo, :user_id)
end
I want to make a button that will allow me to create a new text_field for :phone_number in addition to the one I already have. For example if I want to submit two phone number instead of one in separate fields how would that be accomplished? In the code below I have begun to make a button_to but am not sure what actions to take in order to make a new field of entry. thanks.
<div>
<%= form_for #reminder do |f|%>
<%= f.label "Your Reminder"%>
<%= f.text_area :text %>
<!-- button here to make new text_field to input in an extra phone number -->
<%=button_to "add another number", %>
<%= f.label "Phone Number" %>
<%= f.text_field :phone_number %>
<%= f.label "When to be sent" %>
<%= f.text_field :time%>
<%= f.label "Picture URL" %>
<%= f.text_field :picture %>
<%= f.label "Favorite?" %>
<%= f.check_box :favorite %>
<%= f.submit %>
<% end %>
</div>
You can accomplish this using JavaScript. Get the id of the button and then add the new input to the page when clicked.
I have a nested form where an workout has many exercises, and I'm trying to allow the user to delete and add exercises to the form without having to submit the form.
<%= form_for(#workout) do |f| %>
<%= f.label :name %><br />
<%= f.text_field :name %><br />
<%= f.label :description %><br>
<%= f.text_area :description %></br>
<%= f.fields_for :workout_exercises do |workout_exercise| %>
<fieldset>
<%= workout_exercise.text_field :name %>
<%= workout_exercise.hidden_field :_destroy %>
<%= link_to "Remove Exercise", '#', class: "remove_fields" %>
##This is what I want to do ##
<%= link_to 'Remove Exercise', set, remote: true, class: "remove_fields delete", method: :delete %>
</fieldset>
<% end %>
Right now I can make the fields fade out, but to save the change to the DB I need to click the "update workout" button at the bottom of the form. I want the change to take effect as soon as the "Remove Exercise" button is pressed, otherwise, users will think an element has been deleted when it's actually just been hidden.
Any ideas?
Thanks!
I would strongly recommend the Cocoon gem for this. You can add events before and after a nested fieldset is inserted as needed.
I have a basic search form in my rails app that records each search that has been entered. I also want my users to be able to initiate a search with a url in the browser. E.g. http://www.example.com/searches?q=foo
I've played around with the different routing options and the logic in my controller but I can't seem to get this form:
<%= form_for(#search) do |f| %>
<div class="field">
<%= f.label :search %><br />
<%= f.text_field :search %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
To initiate a POST action on the database and submit a new search.
Thanks :)
Change your form to do a GET request instead of a POST then you can use both the form and the url:
<%= form_for(#search), :method => :get do |f| %>
(I'm just learning rails so....)
I have a photo model and a gallery model, habtm associations and a join table. I'm making a photo gallery. The gallery page has a title field and description field. User creates gallery title, then goes to the photo page and checkboxes each image they want in that gallery.
I get the error "undefined method `to_i' for ["1", {"title"=>"1"}]:Array" when trying to save/update a photo with a gallery title(with the checkbox)
<% form_for #photo, :html => {:multipart => true } do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :title %>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :description %>
<%= f.text_area :description %>
</p>
<p>
<%= f.label :image %>
<%= f.file_field :image %>
</p>
<% for gallery in #photo.galleries %>
<% fields_for "painting[gallery_attributes][]", gallery do |g| %>
<div>
<%= g.check_box :title %>
<%= gallery.title %>
</div>
<% end %>
<% end %>
<p><%= submit_tag 'Update' %></p>
<% end %>
How much of this is horribly wrong?
Can someone point me in the right direction?, I can't find any tutorials relating to this for 2.3 and above.
For this complicated task of having multiple models updated with a single form, i would recommend the three part series in railscasts:
http://railscasts.com/episodes/73-complex-forms-part-1
http://railscasts.com/episodes/74-complex-forms-part-2
http://railscasts.com/episodes/74-complex-forms-part-3
Though please if another forum member knows of a better/more up-to-date tutorial let me know, i did this back in the day and it was a pain.
I thought this was a good example of nested forms http://github.com/alloy/complex-form-examples/