我正在尝试创建一个正常运行的form_for
书签(嵌套在主题下)。当我尝试转到页面时,我看到形式中的第一个参数不能包含nil或为空错误。我不确定如何解决这个问题。
到目前为止,这是我的代码:
topics_show 中的表单:
<div class="col-md-8">
<%= form_for [@topic, @bookmark] do |b| %>
<div class="form-group">
<%= b.label :url %>
<%= b.text_field :url, class: 'form-control', placeholder: "Enter URL Here" %>
<%= b.submit "Save Topic", class: 'btn btn-success' %>
</div>
<% end %>
</div>
BookmarksController :
def create
@topic = Topic.find(params[:topic_id])
@bookmark = Bookmark.new(bookmark_params)
end
def new
@topic = Topic.find(params[:topic_id])
@bookmark = Bookmark.new
render :show
end
def bookmark_params
params.require(:bookmark).permit(:url, :topic_id)
end
TopicsController :
def new
@topic = Topic.new
end
def create
@topic = Topic.new(topic_params)
if @topic.save
flash[:notice] = "Topic was successfully saved."
redirect_to @topic
else
flash.now[:alert] = "Error saving topic. Try again."
render :new
end
end
def topic_params
params.require(:topic).permit(:title)
end
最后,我在routes.rb文件中的主题中嵌套了书签。
感谢您的帮助,谢谢。