Rails 4 edit form do not display upload photo - ruby-on-rails-4

I have a mongo rails 4 app, with an embedded photo.
I have no problem in the new form, but in the edit form, if the user has not upload any photo, the form will not display a "add photo button".
Here is the form:
<%= f.fields_for :founder_profile_photos do |founder_photo_f| %>
<%= render partial: 'founder_profile_photo_fields', locals: { f: founder_photo_f } %>
<%= link_to_add_association raw('<i class="fi-plus"> add a founder photo</i>'), f, :founder_profile_photos %>
<% end %>
Here is the partial:
<div class="nested-fields">
<% if f.object.file.to_s.empty? %>
<%= f.file_field :file, label: "Upload a founder photo." %>
<% else %>
<%= image_tag f.object.file, class: 'small-10 medium-10 image-previewer' %>
<% end %>
</div>

Thank you so much for your help. I finally found what was wrong, I needed to build the field photo in the edit action of my controller, other the field does not exist and therefore do not appear in the view.
def edit
if #user.photos.empty?
#user.photos.build
end
end
photo is embedded into the user model (one to many relation) if you have a one to one relation you have to do:
if #user.photo.blank?
#user.photo = Photo.new
end
Thank you very much for all your help, still so much to learn.

Related

Will_paginate, Ruby on Rails, conditions

I am using will_paginate for my Rails project. Depending on 'Post' variable its 'mediatype' attribute I use either memo or photo and show results in different parts of the page. Will_paginate paginates both sides of the website together. If I have done 10 posts: 1 memo, 9 posts of photos, and 1 memo and I want to show 5 results per page, I will see on the front page one memo instead of two, and 4 photos. I use the same variable post and I have two different attributes how to make separate paginations for them? Here is my code. In fact I would like to add 'load more' button. It might a bit too complicated, so I will be satisfied with simple version of pagination. Still if you know how to make 'load more' buttons for two parts of the website, I will much appreciate. Thank you.
INDEX VIEW
<% content_for :memo_content do %>
<%= will_paginate #posts %>
<% #posts.reverse_each do |post| %>
<% if post.mediatype == "Memo" %>
<%= post.mediatype %>:
<%= post.title %>;
<% end %>
<% end %>
<% end %>
<% content_for :media_content do %>
<% #posts.reverse_each do |post| %>
<% if post.mediatype == "Photo" %>
<%= post.mediatype %>:
<%= post.title %>;
<% end %>
<% end %>
<% end %>
POSTS_CONTROLLER
def index
#posts = Post.order(name: :asc).paginate(page: params[:page], per_page: 5)
end
you can get count manually in controller and pass this count to will_paginate
Like:
#posts = Post.order(name: :asc).paginate(page: params[:page], per_page: 5)
from #posts get manually for both memo and posts of photos and pass this count in view.
<%= will_paginate #memo_count, :renderer => BootstrapPagination::Rails %>
<%= will_paginate #posts_of_photos_count, :renderer => BootstrapPagination::Rails %>

Ruby on rails Tutorial Missing partial articles/_form

Hi I have just started using Ruby on rails and have been following the tutorial # http://guides.rubyonrails.org/getting_started.html.
The problem I have got, is with 5.12 Using partials to clean up duplication in views.
I am getting the error ActionView::MissingTemplate in Articles#new.
It is looking for _form.html.erb , which is in the directory /app/views/articles/. So not sure why it cannot find it.
articles controller for new
def new
#article = Article.new
end
articles view for new
<h1>New article</h1>
<%= render 'form' %>
<%= link_to 'Back', articles_path %>
articles view for _form.html.erb
<%= form_for #article do |f| %>
<% if #article.errors.any? %>
<div id="error_explanation">
<h2>
<%= pluralize(#article.errors.count, "error") %> prohibited
this article from being saved:
</h2>
<ul>
<% #article.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<p>
<%= f.label :title %><br>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :text %><br>
<%= f.text_area :text %>
</p>
<p>
<%= f.submit %>
</p>
< % end %>
Any help would be appreciated, need to know how to solve before going on to next part.
Hi I have got it to work by renaming _form.html.erb to_form as the error is looking for /apps/views/_form. I don't know if this is the correct way to solve the problem, as its says in the tutorial to name the file _form.html.erb.
would this cause issues in future doing it this way?
Check the file name, maybe you copied it wrong
I changed the file name to '_form.erb' and it worked. Still puzzled why the full suffix of 'html.erb' didn't work.

Enter data in rails before controller action

I have a transactions controller and view. When I click to link "Transactions", I show all transactions.
How to add a before display transactions such as enter start and end dates?
What you need is probably the ransack gem.
You can add a custom filtering mechanism in your controller like this:
def index
#q = Transaction.ransack(params[:q])
#transactions = #q.result(distinct: true)
end
and using in the view a simple for like this:
<%= search_form_for #q do |f| %>
<%= f.label :start_date %>
<%= f.search_field :created_at_gteq %>
<%= f.label :end_date %>
<%= f.search_field :created_at_lteq %>
<%= f.submit %>
<% end %>
And this will let you filter the Transaction data using the created_at param as a filter. Modify the form accordingly if you need to filter using another Date/DateTime param from the model.

Conditioning based on checkbox status

When a potential User is editing their Profile, they have an option to show/hide some information from public view. I did that with
<div class="field">
<%= f.text_field :contact %>
<%= f.select(:contact_type_id, ContactType.all.map {|l| [l.name, l.id]}) %>
<%= f.check_box :visible %>
<%= f.label :visible, "Vidljivo" %>
<%= f.link_to_remove "IzbriĆĄi kontakt" %>
</div>
in a partial that's called in the view like this
<%= f.fields_for :contacts do |l| %>
<%= render 'contacts_form', f: l %>
<% end %>
What I now want is to display on a public profile page only that information that the User marked as visible, and I tried it through a different partial
<div class="field">
<% if :visible == true %>
<%= f.text_field :contact %>
<% end %>
</div>
called from
<%= f.fields_for :contacts do |l| %>
<%= render 'contacts_show', f: l %>
<% end %>
but it doesn't work. I tried tons of variations, but it all came down to guessing the right syntax. How do I display only those entries which visibility is marked as "true"?
Why are you displaying the public profile as a form? If you only want to show the information you can iterate over the contact information of the user where the contact type is visible.
I solved it by using a different approach. In my user controller I defined an instance variable #visible and passed it the array consisting of all database entries that have visible parameter set to true. Then I used #visible.each in the view to print the desired content.

Rails 4.0 link_to overlaying an image with a link

I have a thumbnail image that links to a blog post entry which is accompanied by a text title. The CSS shades the image slightly and when the user hovers over the image, it re-sizes the image slightly. The user can click on the title or on the thumbnail to take them to the entry.
I cannot get the overlay tag here:
to be generated within rails link_to
<%= link_to(:class=>"overlay overlay-primary", id: entry) %>
The image is part of a figure
<figure class="entry-thumbnail">
<!-- to disable lazy loading, remove data-src and data-src-retina -->
<% if entry.cover_photo_link.blank? %>
<%= image_tag "placeholder.gif" %>
<% else %>
<%= image_tag entry.cover_photo_link %>
<% end %>
<!--fallback for no javascript browsers-->
<noscript>
<% if entry.cover_photo_link.blank? %>
<%= image_tag "placeholder.gif" %>
<% else %>
<%= image_tag entry.cover_photo_link %>
<% end %>
</noscript>
</figure>
The full thumbnail codes is in this gist
How do I get the image to link to use link_to render this?
The solution was simple. Putting the a blank "" in link_to did the trick
<%= link_to "", entry, :class => "overlay overlay-primary" %>
renders:
<a class="overlay overlay-primary" href="/entries/5"></a>
This worked well.