user5198598
user5198598

Reputation:

Heroku getting wrong ID

so I am using heroku to give demo's of my ruby on rails applications, and make my personal website. However with a live heroku app, the url articles/2 instead of getting the ID for the post, is trying to get the user_id. So say if you have 5 articles, but 3 users. Then you try to go to articles/5, it will give you an error, with this in the log.

5-12-04T10:03:51.370101+00:00 app[web.1]: Completed 404 Not Found in 6ms (ActiveRecord: 4.3ms)

2015-12-04T10:03:51.361893+00:00 app[web.1]: Started GET "/articles/3" for 46.237.130.242 at 2015-12-04 10:03:51 +0000

2015-12-04T10:03:51.513724+00:00 app[web.1]: Parameters: {"id"=>"3"} 2015-12-04T10:03:51.513671+00:00 app[web.1]: Processing by ArticlesController#show as HTML 2015-12-04T10:03:51.516207+00:00 app[web.1]: User Load (1.6ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 3]]

2015-12-04T10:03:51.518725+00:00 app[web.1]: 2015-12-04T10:03:51.518728+00:00 app[web.1]: ActiveRecord::RecordNotFound (Couldn't find User with 'id'=3):

2015-12-04T10:03:51.518729+00:00 app[web.1]:
app/controllers/articles_controller.rb:31:in `show'

2015-12-04T10:03:51.518730+00:00 app[web.1]: 2015-12-04T10:03:51.518730+00:00 app[web.1]: 2015-12-04T10:03:51.364367+00:00 app[web.1]: Parameters: {"id"=>"3"}

The code for the show method in the article controller.

def show
  @user = User.find(params[:id])
  @article = Article.find(params[:id])
  @comment = Comment.new
end

The code for my show.html.erb

<h1 align="center">Title: <%= @article.title %></h1>

<div class="well col-xs-8 col-xs-offset-2">
<div class="row">
    <div class="col-xs-4 col-xs-offset-4">
        <div class="well well-sm">
            <div class="user-img">
                <p class="created">Created By:</p>
                <%= gravatar_for @article.user, size: 150 %>
            </div>
            <div class="user-title">
                <%= link_to @article.user.username,  user_path(@article.user) %> <br />
            </div>
        </div>
    </div>
</div>
<h4 class="center description"><strong>Description:</strong></h4>
<hr />
    <%= simple_format(@article.description) %>
<div class="article-actions">
    <% if logged_in? && current_user == @article.user %>
    <%= link_to 'Edit', edit_article_path(@article), class: "btn btn-     xs btn-primary" %>
        <%= link_to 'Delete this article', article_path(@article),   method: :delete,
                                                                           data: { confirm: "Are you sure you want to delete this article?" },
                                                                             class: "btn btn-xs btn-danger" %>
    <%= link_to 'View all articles', articles_path, class: "btn btn-xs     btn-success" %>
       <% else %>
       <%= link_to 'View all articles', articles_path, class: "btn btn-xs btn-success" %>
        <% end %>
    </div>

Also the create method:

def create
redirect_to articles_path if !logged_in?
@articles = Article.all
@article = Article.new(article_params)
@article.user_id = current_user.id
if @article.save 
        flash[:success] = "Post created successfully!"
        respond_to do |format|
            format.html { redirect_to articles_path }
            format.js
        end
else
    flash[:danger] = "We could not create you're article!"
    render 'new'
end

end

Upvotes: 1

Views: 151

Answers (3)

Varun Gupta
Varun Gupta

Reputation: 1

Yes this code will let you find the user first because

@user = User.find(params[:id])

in your code you are first finding the user with :id = 3 , which will not be in database , So this will through error "ActiveRecord::RecordNotFound (Couldn't find User with 'id'=3):"

So, either remove the first line of your code or else if you want to find "article/3" you must create route differently as "users/:user_id/articles/:id"

using nested resources

and change code as

def show
  @user = User.find(params[:user_id])
  @article = Article.find(params[:id])
  @comment = Comment.new
end

Upvotes: 0

user5198598
user5198598

Reputation:

Let's make this a solution, as you can see this simple mistake has caused an error that took me a little while to figure out. The error being in the show method;-

def show
  @user = User.find(params[:id])
  @article = Article.find(params[:id])
  @comment = Comment.new
end

The error here is that params[:id] is used for finding the ID in the URL for example using the @user = User.find(params[:id] caused the @user variable to be set to whatever was in the URL for example: articles/5 was setting the user_id to 5.

To sort out this error changed the show method to the following:

def show
  @article = Article.find(params[:id])
  @user = User.find(@article.user_id)
  @comment = Comment.new
end

This sets the @user variable to find the article user_id, instead of setting it to the article id instead. However before you can call this method, you have to define @article first otherwise this would fail as you are trying to call the @article instance before defining this.

This is how I managed to figure it out, hope it atleast helps someone else out.

Upvotes: 1

John Beynon
John Beynon

Reputation: 37507

looks like your controller is incorrect:

def show
  @user = User.find(params[:id]) <!==== Is this the problem?
  @article = Article.find(params[:id])
  @comment = Comment.new
end

Upvotes: 0

Related Questions