*Show* view always shows the first pin (item) in the database - ruby-on-rails

I'm working on a pinterest-like app, and I've encountered a problem recently.
When as a logged in user I try to access a pin through "show" it gives me the correct id number in the url for e.g. http://localhost:3000/pins/7 but the description comes from the first item in the database.
Here's my Show view code:
<%= image_tag #pin.image.url %>
<p>
<strong> Description: </strong>
<%= #pin.description %>
</p>
<% if #pin.user == current_user %>
<%= link_to 'Edit', edit_pin_path(#pin) %>
<% end %>
<%= link_to 'Back', pins_path %>
On the other hand when I try to access it via heroku through show or edit it ends with a following message:
"We're sorry, but something went wrong. If you are the application owner check the logs for more information."
Here is my github repo https://github.com/LeJaques/myfirstapp_new
I would be grateful for help!

Your issue seems to be in your set_pin method in your PinController.
private
# Use callbacks to share common setup or constraints between actions.
def set_pin
#pin = Pin.find_by(params[:id])
end
When passing simply params[:id] to find_by, you're going to run into issues. You should either use
#pin = Pin.find(params[:id])
or
#pin = Pin.find_by(id: params[:id])
the latter of which you oddly used in your later correct_user method.
Unrelated to your question, but in the future, rather than linking to your github project and having us dig through the code, please post the relevant code to your question. Doing so will help you get quicker answers in the future; this question would have been answered within five minutes (rather than in over half an hour) if your set_pin method had been in your question from the start.

Looks like you are using find_by to set the #pin incorrectly.
def set_pin
#pin = Pin.find_by(params[:id])
end
Try to change it to just Pin.find(params[:id]) or add a field to search by Pin.find_by(id: params[:id])

Related

Couldn't find Object with 'id'=

stumped here, probably due to my rails noobness.
I am building a 2 sided market place with bids. using devise for 2 user types relevant here, clients and guides. clients build a trip, guides can view trips and then bid on the trip. i've created a custom landing page for the guides (home) once they sign in which lists client trips, i use a custom view and method in my guides controller to do this, no problem.
however, when from that page i try to go to the next custom page for the guide to view the details of the trip (and then submit a bid), suddenly i get a "Couldn't find Trip with 'id'=" error related to the Trip.find(params[:id]) (if i hardcode Trip.find(1) it works).
guides controller
class GuidesController < ApplicationController
def home
#trips = Trip.all
#guide = current_guide
end
def view_trip
#trip = Trip.find(params[:id])
#guide = current_guide
end
end
guide's home page (which is where the guide clicks to view the trip)
<p>Guides's Email: <%= #guide.email %></p>
<% #trips.each do |trip| %>
<li>Location: <%= trip.location.description %></li>
<li>Details <%= trip.details %></li>
<p><%= link_to "View Details and Bid", guides_view_trip_path(trip) %></p>
<% end %>
and then the error output at the page when i try to visit it/click on the link: amazonaws.com/guides/view_trip.1
Couldn't find Trip with 'id'=
Extracted source (around line #21):
19 def view_trip
20
21 #trip = Trip.find(params[:id])
22 ##trip = Trip.find(1)
23 #guide = current_guide
24 end
Request
Parameters:
{"format"=>"1"}
Change this line:
<p><%= link_to "View Details and Bid", guides_view_trip_path(trip) %></p>
By
<p><%= link_to "View Details and Bid", guides_view_trip_path(id: trip.id) %></p>
Your code should be like that
class GuidesController < ApplicationController
def home
#trips = Trip.all
#guide = current_guide
end
def view_trip
#trip = Trip.find(params[:format])
#guide = current_guide
end
end
I don't know what is your routes.rb design, if you need to take this URL with rails default RESTful URL then your routes.rb looks like this
get 'guides/view_trip/:id(.:format)', to: "guides#view_trip", as: :guides_view_trip
#=> guides_view_trip GET /guides/view_trip/:id(.:format) guides#view_trip
Now you can use the view like below
<%= link_to "View Details and Bid", guides_view_trip_path(trip) %>
#=> /guides/view_trip/1
If you use guides_view_trip_path(id: trip.id) then URL looks like this amazonaws.com/guides/view_trip?id=1 and if you use RESTful which I described then looks like this amazonaws.com/guides/view_trip/1
Or you can use Trip.find(params[:id]) to Trip.find(params[:format])
Hope it helps

Rails `edit_joke_path` Links to the Show View, Not the Edit View

I have what should be a fairly simple link_to situation:
<% #jokes.each do |joke| %>
...
<%= link_to edit_joke_path(joke) do %>
<span style="color: blue" class="glyphicon glyphicon-pencil" aria-hidden="true"></span><span style="color: blue">Edit Joke</span>
<% end %>
...
<% end %>
However, when I click this link it takes me to the jokes#show page not the jokes#edit page. Here are my routes:
jokes GET /jokes(.:format) jokes#index
POST /jokes(.:format) jokes#create
new_joke GET /jokes/new(.:format) jokes#new
edit_joke GET /jokes/:id/edit(.:format) jokes#edit
joke GET /jokes/:id(.:format) jokes#show
PATCH /jokes/:id(.:format) jokes#update
PUT /jokes/:id(.:format) jokes#update
DELETE /jokes/:id(.:format) jokes#destroy
Can anyone see where I'm going wrong? This seems really simple, but it's not working the way it should.
ADDITIONAL INFO
Here's the edit action from my jokes_controller:
def edit
#joke = Joke.find(params[:id])
#joke.user = current_user
#joke.save
redirect_to joke_path
end
As per below code in jokes_controller.rb
def edit
#joke = Joke.find(params[:id])
#joke.user = current_user
#joke.save
redirect_to joke_path
end
redirect_to joke_path cause the issue.
So, remove or comment below line in your edit action it will resolve your issue.
redirect_to joke_path
edit_joke_path(joke)
goes to jokes#edit but at the end of the edit fuction you have added redirect_to. Remove redirect_to from the edit function.
redirect_to: Redirects the browser to the target specified
Remove redirect_to joke_path from your action. That's taking you to the show page.

Destroying UpVotes on acts_as_votable

I have a rails app that is working fine using acts_as_votable. The like button upvotes the post count, and then switches to an un-like button and this down votes the post count.
My issue is, that since I started using the Public Activity gem, I can't find a way to remove likes from the feed. I have used the following loop in the activities index view:
<% #activities.each do |activity| %>
<p>
<% if activity.trackable %>
<%= link_to activity.owner.name, activity.owner %>
<%= render_activity activity %>
<% end %>
</p>
<% end %>
When I delete a comment, the entire line in the activity feed of 'FOO added a comment on BAR' disappears. However, because the acts as votable gem actually creates a downvote rather than destroying the upvote, the line 'FOO liked BAR' still appears and would be subsequently followed by 'FOO unliked BAR'.
Does anybody know how I can locate the upvote by the current_user on a particular post and then destroy it?
Below is my controller code for like and unlike as it stands:
def like
#ink.create_activity :like, owner: current_user
#ink.upvote_by current_user
redirect_to :back
end
def unlike
#ink.downvote_by current_user
redirect_to :back
end
Thanks
I know this have been answered but the ideal and easiest answer would be using the official gem method which is unvote_by it works for both upvotes and downvotes.
It looks like what you want to do is remove the model's like notification when it is "unliked" - is that correct? All you have to do is find the relevant activity and destroy it. Because Activities are models just like any other, you can do this with destroy and/or destroy_all.
I'm not quite sure what your public_activity model looks like, so instead of giving a specific example I'll link you to this post on the mailing list for the gem, which shows examples of deleting public activity records.
You may also find it useful to delete the record by its trackable_id - for example:
#activity = PublicActivity::Activity.find_by(trackable_id: (params[:id]), trackable_type: controller_path.classify)
#activity.destroy
There's more information about how that works in this SO answer.
For anyone that stumbles across this in future, thanks to Element119 I was eventually able to pinpoint the current users likes on a particular post with a few variables and arel searches. I then destroyed the pinpointed likes.
def like
#post.create_activity :like,
owner: current_user,
recipient: #post.user
#post.upvote_by current_user
redirect_to :back
end
def unlike
#post.downvote_by current_user
#currentUserLikes = PublicActivity::Activity.where(trackable_id: #post.id, owner_id: current_user.id, key: "post.like")
#currentUserLikes.destroy_all
redirect_to :back
end

rails3 show not getting :user_id in one case

I have a strange problem. I've been coding in Rails for, off and on, a year. I created a new project recently and used scaffolding. Things were going fine, yesturday I started implementing some favoriting features. Now I have a strange problem. I rolled back the stuff I did last night but still have the problem. First
Entry belongs to user
User has many entries
My Entry show method in my controller is very standard and simple
def show
#user = User.find(params[:user_id])
#entry = #user.entries.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render :json => #entry }
end
end
When I view the entry from a normal link in the entries index
<%= link_to 'Show', user_entry_path(#user, entry) %>
I takes me to where it should go:
/users/4/entries/11
When I create new things still look good
/users/4/entries/new
Until I click "create entry" or the submit button
<div class="actions">
<%= f.submit %>
</div>
Then it goes to
/entries/20 ...with the error:
ActiveRecord::RecordNotFound in EntriesController#show
Couldn't find User without an ID
If I go back to the entries index however, the file new entry is there and the show link takes me to the right place. Thoughts? Your help is appreciated!
The error message tells you that User.find(params[:user_id]) couldn't find a user with that ID. Try checking the structure of the GET parameters in the server logs.
If your GET path is /entries/20, then the path only has an entry ID and is missing a user ID. You might be able to fix this in your Controller#create by having it redirect to user_entry_path instead of entry_path.
How does your form look like?
I think you have nested routes? Your form should look like following:
<%= form_for [#user, #entry] do |f| %>
<% # your fields %>
<% end %>
Your form seems to point to resources entry, instead of the nested ressource..

Nested resource issue

I am struggling to pass an id successfully into my URL for the nested resource I have set up called Jobs.
The error I am getting when I try to pass the #job object into my link is as follows:
No route matches {:action=>"edit", :controller=>"jobs", :user_id=>1, :id=>nil}
Which clearly shows it can't find the id correctly and so is finding nil
At the moment I have my routes setup as so:
resources :users do
resources :jobs
end
and the link I have is <%= link_to "Edit", edit_user_job_path(#user.id,#job) %>
What is interesting is that if I pass the object #jobs with an 's' on the end it will load the page correctly but when I click on the link will try and add all of that users job id's.
In my controller for edit I have:
def edit
#user = current_user
#job = #user.jobs.find(params[:id])
end
Any help really would be much appreciated :)
UPDATE
Okay I was defining the object on the wrong page of my controller (under edit instead of index). The issue I am now having is Couldn't find Job without an ID
I updated my controller index definition to:
def index
#user = current_user
#jobs = #user.jobs.all
#job = #user.jobs.find(params[:id])
end
And have in my view (jobs#index)
<% #jobs.each do |f| %>
...
<%= link_to "Edit", edit_user_job_path(#user.id,job) %>
...
<% end %>
Any advice would be much appreciated if you know where I am going wrong :)
That error means that #job is nil.
The link is to the edit path, and the controller code you've provided is from the edit action in the controller. It seems unlikely that the edit page links to itself.
Look at the code that's actually rendering that page (it will appear in your stack trace) and you'll find that #job is not set. I suspect that you are on the index page and have something like:
<% #jobs.each do |job| %>
...
<%= link_to "Edit", edit_user_job_path(#user.id,#job) %>
...
<% end %>
If that is the case, then the link should be to job, not #job, i.e.
<%= link_to "Edit", edit_user_job_path(#user.id,job) %>
(expanding on iHiD's comment with his own post)
Using the restful resources means that you are going with the rails defaults, which consequently means that the index page gives you a list of all Jobs, and by default no single special job. If you run rake routes from the command line, you get all the routes, with parameters that are set from the URI. It should give you something like this:
user_jobs GET /users/:user_id/jobs(.:format) jobs#index
As you can see, there is no :id (params[:id]) for the index action.

Resources