Active Storage Can't resolve image into URL error - ruby-on-rails

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

Related

Ruby on Rails Form Validations Flash on Page Reentry

This is a rather small thing, but rather annoying.
When I submit a blank "create account" form (bringing up form validation errors & styles) and then navigate to another page and then back to the create account page the form validation errors are still there for a half second before the page refreshes.
Is there any way to clear these validations when the page is left so that they don't reappear for a half second when the page is navigated back to?
View Code:
<section class="form form_create_account">
<h2 class="form_header">Create Account</h2>
<%= form_for(#user, url: create_account_path) do |f| %>
<%= render 'shared/error_messages' %>
<%= f.label :first_name %>
<%= f.text_field :first_name %>
<% if #user.errors[:first_name].any? %>
<p class="error_message"><%= #user.errors[:first_name].first %></p>
<% end %>
<%= f.label :last_name %>
<%= f.text_field :last_name %>
<% if #user.errors[:last_name].any? %>
<p class="error_message"><%= #user.errors[:last_name].first %></p>
<% end %>
<%= f.label :email %>
<%= f.text_field :email %>
<% if #user.errors[:email].any? %>
<p class="error_message"><%= #user.errors[:email].first %></p>
<% end %>
<%= f.label :password %>
<%= f.password_field :password %>
<% if #user.errors[:password].any? %>
<p class="error_message"><%= #user.errors[:password].first %></p>
<% end %>
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation %>
<% if #user.errors[:password_confirmation].any? %>
<p class="error_message"><%= #user.errors[:password_confirmation].first %></p>
<% end %>
<%= f.submit "Create Account" %>
<% end %>
</section>
Sometimes rails do it automatically, but sometimes we need to do. When working with flash give special attention on render & redirect.
When you render, you want to set flash.now, instead of the normal flash.
(Ex: flash.now[:error] = "There was a problem" with render, not flash[:error] = "There was a problem".)
If you don't, then the flash message may not show up on the page that's rendered, and it will show up on the next page that's visited.
This answer is for not showing flash[:errors]. You can refer this with hiding flash as well.
Hope this will help

Rails form_for added empty model

I have started the "Get Started Guide" from the ruby on rails website. Everything works fine, but when I change the order of showing all comments and than display the comments-form in the other way round, than the form_forfunction adds a empty comments model to #post.comments and so, I display one empty comment in the loop.
Here is the view:
<h1><%= #post.name %></h1>
<p><%= #post.text %></p>
<h2>Add comment</h2>
<%= form_for([#post, #post.comments.build]) do |f| %>
<p>
<%= f.label :name %><br>
<%= f.text_field :name %>
</p>
<p>
<%= f.label :email %><br>
<%= f.text_field :email %>
</p>
<p>
<%= f.label :text %><br>
<%= f.text_area :text %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
<h2>Comments</h2>
<%= render #post.comments %>
The loop display two comments. One, that exists in the db and one, that has just empty attributes. If I delete the form, than all is shown up correct.
You can select only your persisted comments:
<%= render #post.comments.select(&:persisted?) %>
When you do post.comments.build against some post, it will be added to the post.comments collection and will be displayed along with other comments.
You can always use persisted to check if the object is present in database meaning id is assigned to it.
#post.comments.select(&:persisted?)
Note: .present? check donot work here so you have to use .persisted.?
present check only assosiated to the parent.

Rails gives a InvalidAuthenticityToken error. If I reload the page it works

Here's how my application works : User uploads a photo > He gets redirected to a page with a list of photos > The user set titles and descriptions of photos and hit save. Now sometimes when a user hit save Rails give InvalidAuthenticityToken error. But if the user reload the page before he hit save, Rails won't give the error and everything will work fine.
Why is it happening like this? I use devise and omniauth.
The form :
<%= form_tag update_submits_photos_path, method: :put do %>
<% #submits.each do |submit| %>
<div class="cont">
<%= fields_for "submits[]", submit do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="row">
<div class='subDetails'>
<%= f.label :title, 'Title :', class: "updLbl" %>
<%= f.text_field :title, class: "updInp" %>
<%= f.label :description, 'Description :', class: "updLbl" %>
<%= f.text_field :description, class: "updInp" %>
</div>
</div>
<% end %>
</div>
<% end %>
<%= submit_tag "Update All",class: 'btn btn-primary btn-lg uploadedUp' %>
<br>
<% end %>
Update : I just updated to Rails 4.2 stable and now the problem have disappeared!
There are two paths you can follow in order to resolve the issue.
First is add following line:
skip_before_filter :verify_authenticity_token, only: :the_action
to the corresponding controller, where :the_action is most likely update.
Second option is to add the token to your form:
<%= hidden_field_tag :authenticity_token, form_authenticity_token -%>

Work after user creation

I create a new user with basic fields as user name and password. Then I redirect to another page where the user is allowed to complete his profile. The page looks like this.
Ok, I created the user first. Then I redirected the user to complete his profile. The profile page looks like this
<h1>User successfully created</h1>
<h3>Would you like to complete your profile now?</h3>
<%= render #profile %>
<%= render #address %>
<%= button_to 'Save',{
:controller => "users",
:action => "update",
:id => #user.id,
:profile => #profile,
:address => #address
}, :method => :put %>
The code above executes the update action in the controller. However, I am getting nil for the #profile and #address objects. I guess the button_to is having no effect on the profile and address partials.
The address partial looks as follows
<%= form_for(#address) do |f| %>
<% if #address.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#address.errors.count, "error") %> prohibited this address from being saved:</h2>
<ul>
<% #address.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<fieldset>
<legend>Addresss</legend>
<div class="field">
<%= f.label :street_address %>
<%= f.text_field :street_address, :size => 40 %>
</div>
<div class="field">
<%= f.label :street_address_2 %>
<%= f.text_field :street_address_2, :size => 40 %>
</div>
<div class="field">
<%= f.label :city %>
<%= f.text_field :city, :size => 40 %>
</div>
<div class="field">
<%= f.label :state %>
<%= f.text_field :state, :size => 40 %>
</div>
<div class="field">
<%= f.label :zip_code %>
<%= f.text_field :zip_code, :size => 40 %>
</div>
<% end %>
I would appreciate if anyone would be able to help me on this.
The button_to method only creates a button which an be used to call an controller/ go to another page.
If you want to allow the user to modify fields, you need to use a form. Please read this guide on forms for more information, especially part 2 is of interest for your situation I guess.
Edit: You need a submit button in your form to submit it, not a button_to outside the form. If you want to have both partials submitted at the same time, you need 1 big form and a controller which is able to process the data of both models.

Multi-model form problem

(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/

Resources