【发布时间】: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
行不通。 (它没有:()
我不太确定如何解决此路由问题(尝试谷歌搜索)。谢谢。
【问题讨论】:
-
@Pavan 抱歉,我今天正在开发一个新功能,但没有看到答案。将尝试解决方案并尽快回复!谢谢你的提醒!
标签: ruby-on-rails model-view-controller