简体   繁体   中英

Link works on localhost but not heroku

Weird, everything's been working fine in my application on heroku until today. I must have changed something. I have 3 models: posts, comments and questions. None of the links going to the question index work (but everything works fine running on localhost). The heroku db has been migrated and the urls are going to the right place. Here is the heroku log error I'm getting:

ActiveRecord::RecordNotFound (Couldn't find Question with id=4)

and the pages says:

The page you were looking for doesn't exist.

Here are what the links look like:

<%= link_to (comment.body), comment_questions_path(comment) %>

and here's questions#index:

def index
  @comment = Comment.find params[:comment_id]
  @questions = @comment.questions
  @question = Question.find params[:comment_id]
end

Here are the routes:

 resources :posts do
  resources :comments do
  end
 end

 resources :comments do
  resources :questions do
 end
 end

I think the problem is that it's looking for the question id even though it's the questions index page. The links aren't working from the posts pages or the comments pages. If you need me to post more files let me know in the comments.

This line is wrong in your questions#index :

@question = Question.find params[:comment_id]

You're trying to find a question based on the Comment ID, which probably isn't what you intended. The problem is, there's no other ID that you could use either.

If you run rake routes you can see a list of all the routes generated by your config/routes.rb file. You should see a route like this:

comment_questions GET comments/:comment_id/questions questions#index

This means that when someone visits a url like /comments/4/questions (which can be generated with the helper comment_questions_path ) they will call the index method of QuestionsController , and params[:comment_id] will equal 4. Your code is trying to look up the Comment with ID 4 and the Question with ID 4 - but there isn't a question with ID 4 in your database, so the app crashes.

What is @question supposed to refer to anyway? The questions index page should generally be used to list ALL The questions for a particular comment, rather than any specific one of them.


PS you don't need to include the inner do ... end in your routes:

resources :comments do
  resources :questions do
  end
end

can just be

resources :comments do
  resources :questions
end

您正在使用params[:comment_id]作为question_id params[:comment_id]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM