I have a weird issue that I can't seem to figure out nor know how to google.
Let me explain what the error is before going into the code stuff.
I have an album, which has many photos (photos belong to album).
Album works fine. Photo works fine.
When I try to click on show link of photo ... that's where things seem to go sideways, ie, none of it works after the first one.
Any ideas? Please let me know if further information is needed.
The problem seems to be coming from my Photos controller of show action, ie this:
# app/controllers/photos_controller.rb
def show
#album = Album.find(params[:album_id])
#photo = #album.photos.find(params[:id])
end
The error that is being generated is this:
ActiveRecord::RecordNotFound in PhotosController#show
Couldn't find Album with 'id'=4
The weird part is that the first photo of ablum works. It's all other subsequent photos that don't work.
This this the app/views/photos/show.html.erb:
<h1>Album Details</h1>
<b><p>Title</p></b>
<p><%= #album.title %></p>
<b><p>Descriptions</p></b>
<p><%= #album.description %></p>
<%= link_to "All Albums", albums_path %>
<h3>Photos</h3>
<% #album.photos.each do |photo| %>
<p>
<b>Title</b>
<%= photo.title %>
<%= link_to 'Show', album_photo_path(photo) %>
</p>
<% end %>
<h3>Add More Photos</h3>
<%= form_for([#album, #album.photos.build]) do |f| %>
<p>
<%= f.label :title %><br />
<%= f.text_field :title %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
It seems to me that you link_to for showing the photo is missing the id for the album.
Instead of
<%= link_to 'Show', album_photo_path(photo) %>
Should it be
<%= link_to 'Show', album_photo_path(#album, photo) %>
Since it looks like :album_id is expected as a separate parameter from the photos :id.
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
This is the picture of my problem ---> Duplicate Delete Links Picture
I have a comment with two delete links.
This is the order of how I'm rendering the comment form and the comments from partials:
<h2>Comments</h2>
<%= render 'comments/form' %>
<%= render #recipe.comments %>
This is the code for _form.html.erb
<%= form_for([#recipe, #recipe.comments.build]) do |f| %>
<p><%= f.label 'Comment' %> <br> <%= f.text_area :comment %></p>
<p><%= f.submit 'Post' %></p>
<% end %>
This is the code for _comment.html.erb
<div class="show-text-formatting">
<p><strong><%= comment.name %></strong></p>
<p><%= comment.comment %></p>
<%= link_to 'Delete', [comment.recipe, comment], method: :delete,
data: { confirm: 'Are you sure want to delete this comment?' } %>
</div>
The first delete link have the extension: /recipes/10/comments/1 (1 is the comment id #)
Also, when I hover over the second delete link has the extension: /recipes/10/comment
The difference between is that the second delete link's extension doesn't have a comment id
I know that by switching the order of
<h2>Comments</h2>
<%= render 'comments/form' %>
<%= render #recipe.comments %>
to
<h2>Comments</h2>
<%= render #recipe.comments %>
<%= render 'comments/form' %>
would solve my problem, but I want the form to come before the comments.
EDIT:
The second delete link isn't actually a "second" delete link. It's a delete link for a null object. I still don't know how to get rid of it though.
Try this. Set an order to your comments. Like this:
<h2>Comments</h2>
<%= render 'comments/form' %>
<%= render #recipe.comments.order(created_at: :DESC) %>
That should fix your problem. I've had the same issue and it worked for me.
If you know for sure it's nil object, why don't you add a trigger. I mean we can check the object whether it's nil or not before rendering.
I am working on a project and I just introduced polymorphism for posting text Posts and photo Posts.
When I try to post an image I get this error: "'nil' is not an ActiveModel-compatible object that returns a valid partial path."
The first show file (app/views/dashboards/show.html.erb) with code:
<%= form_for #text_post do |form| %>
<%= form.text_field :body, placeholder: 'Post content here' %>
<%= form.submit 'Post Title!' %>
<%end%>
<%= form_for #photo_post do |form| %>
<%= form.file_field :image %>
<%= form.submit 'Post Image!' %>
<%end%>
<%= render #posts %>
which leads to this at app/views/photo_posts/_photo_post.html.erb with the following code:
<%= image_tag photo_post.image.url(:post) %>
The app/views/posts/_post.html.erb which causes problem is:
<%= div_for post do %>
<%= link_to post.user.username, post.user %>
posted
<%= render post.content %>
<%= link_to time_ago_in_words(post.created_at), post %>
<% end %>
Sorry if the details I provide is insufficient, I am a new ROR developer. If you want, you can fork or check the project from here, (I pushed so you can check for the error).
Any help/guidance is greatly appreciated!
Please try
<%= render post.content if post.content %>
instead of
<%= render post.content %>
as per the error logs, this should fix the issue.
I have a post and it has a title. I want the user to be able to press a button and then after the title the word completed is placed in but I'm not sure how I would do this.
my post view looks like
<% div_for post do %>
<strong><%= link_to_unless_current h(post.title), post %></strong> - <%= link_to post.user.name, post.user %>
<%= simple_format h(post.body) %>
<% end %>
I feel like I may need an if statement saying if clicked original code for the view plus "completed" else original code but I'm not sure how I would use this logic in a button and how to describe this logic. I'm still a noob so I'm sorry if this question is overly simple.
What I want to do is have a button labeled completed that adds the work complete onto the end of the title.
this is my show view
<%= render :partial => #post %>
<% if current_user?(#post.user) %>
<p>
<%= link_to 'Edit', edit_post_path(#post)%>
<%= link_to 'Delete', #post, :method => :delete, :confirm => "Are you sure?" %>
</p>
<% else %>
<% end %>
<h2>Comments</h2>
<div id="comments">
<%= render :partial => #post.comments %>
</div>
<%= form_for [#post, Comment.new] do |f| %>
<p>
<%= f.label :body, "New Comment" %><br />
<%= f.text_area :body %>
</p>
<p><%= f.submit "Add Comment" %></p>
<% end %>
the view in shown in my main question is the view for each individual post so basically I want to add completed in front of <%= link_to_unless_current h(post.title), post %> that block of code when I press the button and then it should display the word completed and the original title
Im not getting your idea if what exactly shoul happen when clicking on the title, but if u want to change stuff on the fly in our html site, u need JS or ajax to handle this...
Could u please send more example code or specify what should happen or where the title should be placed?
these are the codes
<p>
<% form_for #movie do |m| %>
<%= m.label :title, 'Title' %>:
<%= m.text_field :title %></br>
<%= m.label :release_year, 'Release Year' %>:
<%= m.select :release_year, (1900..2011) %></br>
</br>
<% Movie.genres.each do |genre| %>
<%=h genre %>
<%= check_box_tag :genre, genre %></br>
<% end%>
</br>
<%= m.submit "Save" %>
<% end %>
</p>
and my code in the controller:
def create
#movie = Movie.new(params[:movie].merge(:genre))
if #movie.save!
render show_path
else
render new_path
end
But for whatever reason, I keep getting error messages saying "undefined method `each_pair' for :genre:Symbol" or "cannot find Movie without an ID". It's not saving properly.
Is it because of my submit form is only the movie form |m| or is it because my create function in the controller is wrong?
Thanks.
perhaps instead of
params[:movie].merge(:genre)
you wanted to do
params[:movie].merge(:genre => params[:genre])
?
Check Firebug in Firefox (or the Chrome developer tools if you're using Chrome, or your app logs if you're using neither), and see what parameters are actually being passed in your scenario? Sounds like your code is expecting to receive a parameter that is not actually being passed. The tools/log should reveal where the breakdown is happening.