ROR5: 'param is missing or the value is empty' error with adding Bookmarks

sofarsophie

I have an app where users can ask questions and bookmark certain questions. I'm done with the users, questions, and answers, so I've added a BookmarkController & Bookmarks model. At first, I considered using associations, but my app has a few associations already so I'm (or I've attempted at) using query parameters such as user_id and question_id to fetch bookmarks.

The structure is a bit like StackOverflow. A user navigates to a single question view and bookmarks it on that page. This creates a new bookmark model containing the user_id of current_user and the question_id. The user can go to his profile to view all the questions he bookmarked, fetched using his user_id. (Answers cannot be bookmarked. Only questions.)

I've been getting a 'param is missing or the value is empty: bookmark' error, although I have followed similar steps I did for my QuestionsController. It would be great if someone could help me out in identifying what's wrong/bad about my code!

rake routes (first part omitted)

bookmark_question PUT    /questions/:id/bookmark(.:format)                  questions#bookmark
questions GET    /questions(.:format)                               questions#index
                 POST   /questions(.:format)                               questions#create
    new_question GET    /questions/new(.:format)                           questions#new
   edit_question GET    /questions/:id/edit(.:format)                      questions#edit
        question GET    /questions/:id(.:format)                           questions#show
                 PATCH  /questions/:id(.:format)                           questions#update
                 PUT    /questions/:id(.:format)                           questions#update
                 DELETE /questions/:id(.:format)                           questions#destroy

route.rb (excerpt)

# Questions
get '/questions/:id' => 'bookmarks#create'

show.html.erb (questions#show)

<% if current_user %>
  <%= link_to "Bookmark", :controller => 'bookmarks', :action => 'create' %>
<% end %>

BookmarksController

class BookmarksController < ApplicationController 

def new
    @bookmark = Bookmark.new
end

def create
    @question = Question.find(params[:id]) # when I delete this line, I get a new error - "undefined local variable 'params'"

    @bookmark = Bookmark.new(bookmark_params)
    @bookmark.user_id = current_user.id
    @bookmark.question_id = @question.id
    @bookmark.save 
    redirect_to @question
end

def destroy

end

private
    def bookmark_params
       params.require(:bookmark).permit(:user_id, :question_id) 
    end
end

Bookmark model

class Bookmark < ApplicationRecord    
  validates :user_id, presence: true
  validates :question_id, presence: true
end

QuestionsController (at the moment, contains no reference to Bookmarks. I thought so because I did the routing, but this might be where I'm going wrong)

class QuestionsController < ApplicationController
def index
    @questions = Question.all
end

def show
    @question = Question.find(params[:id])
    @answers = Answer.all

    # Delete only appears when no answers
    @deletable = (current_user== User.find(@question.user_id)) && (@question.answers.all.size==0)
end

def new
    @question = Question.new
end

def create
    if logged_in?
        @question = Question.new(question_params)
        @question.user_id = current_user.id
        @question.save 
        redirect_to @question
    else 
        redirect_to login_path
    end
end

def destroy
    @question = Question.find(params[:id])
    @question.destroy

    redirect_to root_path
end

private
  def question_params
    params.require(:question).permit(:picture_url, :country, :educational_level, :topic)
  end

end

profile index.html.erb (just for ref)

<% if (@bookmarks.count == 0) %>
///                   
<% else %>
  <%= @bookmarks.each do |bookmark| %>
    <!-- Show bookmark content here like Question.find(bookmark.question_id) etc -->
  <% end %>
<% end %>

I have looked a the previous qns that have the same error as me. But they were all using associations. I hope to not use associations as the bookmark model only needs to keep a record of the user id and qn id.

UPDATE

So, referring to the answers given, I updated my erb to:

<% if logged_in? %>
  <%= link_to "Bookmark", :controller => 'bookmarks', :action => 'create', bookmark: {user_id: current_user.id, question_id: @question.id} %>
<% end %>

hence specifying the controller and action (and the params) that need to be directed. But rails sends an error:

No route matches {:action=>"create", :bookmark=>{:user_id=>2, :question_id=>4}, :controller=>"bookmarks", :id=>"4"}

So I assume it was a routing problem. As Pavan suggested, I did consider nesting my resources, but the nesting is already one level deep, as such:

resources :questions do
  resources :answers
end

And I reckon doing something like:

resources :questions do
  resources :bookmarks # or resources :bookmarks, only: create
  resources :answers
end

won't work. (And it didn't :( )

I'm not so sure how to get this routing problem fixed (tried Googling). Thanks.

Pavan

param is missing or the value is empty: bookmark

The reason for the error is bookmark_params expects a :bookmark key to be present in the params hash, which in your case is missing since you are not passing any.

Change link_to like below:

<% if current_user %>
  <%= link_to "Bookmark", :controller => 'bookmarks', :action => 'create', bookmark: {user_id: current_user.id, question_id: @question.id} %>
<% end %>

Also, the route get '/questions/:id' => 'bookmarks#create' isn't right and would conflict with this route question GET /questions/:id(.:format) questions#show. I would instead recommend building nested routes

resources :users do
  resources :questions do
    resources :bookmarks, only: [:create]
  end
end

Update:

Along with the above, you should change @question = Question.find(params[:id]) to @question = Question.find(params[:bookmark][:question_id])

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Param is missing or the value is empty ROR

From Dev

param is missing or the value is empty error

From Dev

rails error - param is missing or the value is empty: buyer

From Dev

Rails : error param is missing or the value is empty : annonce

From Dev

Getting "Param is missing or the value is empty: user" error

From Dev

param is missing or the value is empty

From Dev

param is missing or the value is empty?

From Dev

param is missing or the value is empty for:

From Dev

Ajax request causing "param is missing or the value is empty" error

From Dev

Rails 4 error "param is missing or the value is empty:" in create

From Dev

Error "param is missing or the value is empty: personas_x_tipos_persona"

From Dev

Error "param is missing or the value is empty: personas_x_tipos_persona"

From Dev

Ransack Search Error: param is missing or the value is empty: profile

From Dev

param is missing or the value is empty error with patch link_to

From Dev

ParameterMissing param is missing or the value is empty

From Dev

param is missing or the value is empty for: quotes

From Dev

param is missing or the value is empty: user

From Dev

Param is missing or the value is empty for: contact

From Dev

param is missing or the value is empty: center

From Dev

param is missing or the value is empty on creation

From Dev

param is missing or the value is empty: comment

From Dev

param is missing or the value is empty: breads

From Dev

param is missing or the value is empty: conversation

From Dev

param is missing or the value is empty: sale

From Dev

Rails 4.0 param is missing or the value is empty

From Dev

ActionController::ParameterMissing (param is missing or the value is empty: film):

From Dev

Test is returning param missing or value empty

From Dev

Rails: param is missing or the value is empty: article

From Dev

ActionController::ParameterMissing (param is missing or the value is empty: bid)

Related Related

  1. 1

    Param is missing or the value is empty ROR

  2. 2

    param is missing or the value is empty error

  3. 3

    rails error - param is missing or the value is empty: buyer

  4. 4

    Rails : error param is missing or the value is empty : annonce

  5. 5

    Getting "Param is missing or the value is empty: user" error

  6. 6

    param is missing or the value is empty

  7. 7

    param is missing or the value is empty?

  8. 8

    param is missing or the value is empty for:

  9. 9

    Ajax request causing "param is missing or the value is empty" error

  10. 10

    Rails 4 error "param is missing or the value is empty:" in create

  11. 11

    Error "param is missing or the value is empty: personas_x_tipos_persona"

  12. 12

    Error "param is missing or the value is empty: personas_x_tipos_persona"

  13. 13

    Ransack Search Error: param is missing or the value is empty: profile

  14. 14

    param is missing or the value is empty error with patch link_to

  15. 15

    ParameterMissing param is missing or the value is empty

  16. 16

    param is missing or the value is empty for: quotes

  17. 17

    param is missing or the value is empty: user

  18. 18

    Param is missing or the value is empty for: contact

  19. 19

    param is missing or the value is empty: center

  20. 20

    param is missing or the value is empty on creation

  21. 21

    param is missing or the value is empty: comment

  22. 22

    param is missing or the value is empty: breads

  23. 23

    param is missing or the value is empty: conversation

  24. 24

    param is missing or the value is empty: sale

  25. 25

    Rails 4.0 param is missing or the value is empty

  26. 26

    ActionController::ParameterMissing (param is missing or the value is empty: film):

  27. 27

    Test is returning param missing or value empty

  28. 28

    Rails: param is missing or the value is empty: article

  29. 29

    ActionController::ParameterMissing (param is missing or the value is empty: bid)

HotTag

Archive