【问题标题】:ROR5: 'param is missing or the value is empty' error with adding BookmarksROR5:添加书签时出现“参数丢失或值为空”错误
【发布时间】:2017-07-03 09:02:20
【问题描述】:

我有一个应用程序,用户可以在其中提出问题并为某些问题添加书签。我已经完成了用户、问题和答案,所以我添加了一个 BookmarkController & Bookmarks 模型。起初,我考虑使用关联,但我的应用程序已经有一些关联,所以我(或我尝试过)使用查询参数(例如 user_id 和 question_id)来获取书签。

结构有点像 StackOverflow。用户导航到单个问题视图并在该页面上为其添加书签。这将创建一个包含 current_user 的 user_id 和 question_id 的新书签模型。用户可以转到他的个人资料以查看他收藏的所有问题,这些问题是使用他的 user_id 获取的。 (答案不能加书签。只有问题。)

我一直收到“参数丢失或值为空:书签”错误,尽管我已按照我为 QuestionsController 执行的类似步骤操作。如果有人能帮助我找出我的代码有什么问题/坏处,那就太好了!

rake 路线(第一部分省略)

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(摘录)

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

show.html.erb (questions#show)

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

书签控制器

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

书签模型

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

问题控制器 (目前,不包含对书签的引用。我是这么认为的,因为我做了路由,但这可能是我出错的地方)

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

结束

profile index.html.erb(仅供参考)

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

我查看了与我有相同错误的以前的 qns。但他们都在使用关联。我希望不要使用关联,因为书签模型只需要保留用户 id 和 qn id 的记录。

更新

所以,参考给出的答案,我将我的 erb 更新为:

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

因此指定需要定向的控制器和操作(以及参数)。但是rails发送错误:

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

所以我认为这是一个路由问题。正如 Pavan 建议的那样,我确实考虑过嵌套我的资源,但嵌套已经是一层深了,因此:

resources :questions do
  resources :answers
end

我估计会做类似的事情:

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

行不通。 (它没有:()

我不太确定如何解决此路由问题(尝试谷歌搜索)。谢谢。

【问题讨论】:

标签: ruby-on-rails model-view-controller


【解决方案1】:

参数丢失或值为空:书签

错误的原因是 bookmark_params 期望 :bookmark 密钥出现在 params 哈希中,在您的情况下,这是 缺少,因为您没有传递任何内容。 p>

如下更改link_to

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

另外,get '/questions/:id' =&gt; 'bookmarks#create' 的路线不正确,会与question GET /questions/:id(.:format) questions#show 的路线发生冲突。我会建议构建嵌套路由

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

更新:

除了上述之外,您应该将@question = Question.find(params[:id])更改为@question = Question.find(params[:bookmark][:question_id])

【讨论】:

  • 感谢您的回答。当我昨天回到我的代码时,我不得不多次阅读它以记住我尝试过的内容。
  • 但是关于您修复路线的解决方案,关联不起作用 - 一个问题已经有_许多答案,所以当我尝试包含书签时,本地服务器拒绝启动。由于我将 user_id 和 question_id 记录到制作的每个书签中,所以我一开始就不需要任何关联吗?
【解决方案2】:

'param is missing or the value is empty: bookmark,这个错误意味着,你的params 对象中没有bookmark 键,但是你定义了你的bookmark_params 有一个:

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

这就是它抛出上述错误消息的原因。

您应该确保在bookmark 键下发送user_idquestion_id 键/值对。像这样的:

bookmark: { user_id: 1, question_id: 2}.

因此,您的代码应如下所示(将bookmark 添加到params):

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

【讨论】:

  • 感谢您的回答。我已经尝试过该方法并指定了控制器和操作: 'bookmarks', :action => 'create', bookmark: {user_id: current_user.id, question_id: @question.id} %>。但是rails找不到与之匹配的路线。
  • 我尝试通过添加来路由它:资源:问题做资源:书签资源:答案结束但是这种嵌套似乎不起作用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多