Couldn't find User without an ID Error

Asked 1 years ago, Updated 1 years ago, 83 views

"Couldn't find User without an ID" error appears in the code below.

Enter a description of the image here

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

  sort_key=(params[:column].blank?||params[:order].blank?)?{updated_at:'DESC'}:{params[:column]=>params[:order]}

  if@user==current_user
    @origques=Origque.select('origques.*', 'count(quefavorites.id)AS favs')
    .left_joins(:quefavorites).group('origques.id').order(sort_key).where(user_id:params[:id])
    .includes(:quefavorite_users)

  else
    @origques=Origque..select('origques.*', 'count(quefavorites.id)AS favs')
    .left_joins(:quefavorites).group('origques.id').order(sort_key).where(publish_check:true)
    .where(user_id:params[:id]).includes(:quefavorite_users)
  end
end

If you press the sort button from view, the index will fail.
I tried binding.pry and found that params[:id]=2.

@origques=Origque.select('origques.*', 'count(quefavorites.id)AS favs').left_joins(:quefavorites).group('origques.id').order(sort_key).where(user_id:params[:id]).includes(:quefavorite_users)

There seems to be a problem with how to write.

If you know the cause, please let me know.

(additional)

If you search for params[:id] on the error screen, it says nil, so
I think it means that the parameters are not crossed.

I tried binding.pry just below the error point.

Enter a description of the image here

Enter a description of the image here

If you cannot pass User.id from view
Use hidden or something to set parameters to the code below. If I can give it to you, I think I can solve it.

I've looked into many things, but I don't know how to write the code.

<%=form_tag:user_ques,method::get,class:"search top-margin" do%>
  <%params.slice(:column,:order).each do|key,val|%>
    <%=hidden_field_tag key, val%>
  <%end%>
  <%=text_field_tag "q", params[:q], class: "search-design"%>
  <%=submit_tag "sch", class: "btn-search"%>
<%end%>


Add <%=hidden_field_tag:id,@u ser.id%> as shown below. I saw it, but I also saw
Couldn't find User without an ID on the controller. This will result in an error.

<%=form_tag:user_ques,method::get,class:"search top-margin" do%>
  <%params.slice(:column,:order).each do|key,val|%>
    <%=hidden_field_tag key, val%>
  <%end%>
  <%=hidden_field_tag:id,@u ser.id%>
  <%=text_field_tag "q", params[:q], class: "search-design"%>
  <%=submit_tag "sch", class: "btn-search"%>
<%end%>

Enter a description of the image here
The parameter is nil.

The route.rb is written as follows.Enter a description of the image here
form_tag: User_ques passed to view index.

 resources: user_ques, only: [:index,:edit,:update,:destroy] do
    get "search", on::collection
  end


by calling erb using button_to and writing id:params[:id] in it Resolved.

<li><%=button_to"⬇ ,", {controller: 'user_ques', action: 'index'}, { method::get, class: "order-button", param: {column: 'id', id:params[:id], order: 'd';qes}}

ruby-on-rails ruby sql

2022-09-30 21:47

1 Answers


in each erb file <%=hidden_field_tag:id,@u ser.id%>
and
id:params[:id]
It was resolved by adding .


2022-09-30 21:47

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.