在 Rails 上使用 Ruby 保存时遇到问题

Having issues in saving with Ruby on Rails

我正在尝试用 Rails 制作一个待办事项列表,首先我制作了列表和它的规格,一切顺利,但后来我开始制作它的项目,但它很简单'保存它的内容,它存储一个空字符串,它在表单或规格上都不起作用。当我尝试使用 Rails 控制台保存时,一切正常。

这是我从 Rspec 收到的消息:

Creating todo items is successful with valid content
 Failure/Error: expect(page).to have_content("Ruby")
   expected to find text "Ruby" in ""

以上是用于...的代码

#todo_item_controller.rb

class TodoItemsController < ApplicationController
  def index
    @todo_list = TodoList.find{ params[:todo_list_id] }
  end

  def new
    @todo_list = TodoList.find{ params[:todo_list_id] }
    @todo_item = @todo_list.todo_items.new
  end

  def create
    @todo_list = TodoList.find(params[:todo_list_id])
    @todo_item = @todo_list.todo_items.new(todo_item_params)
    if @todo_list.save
      flash[:success] = "Added todo list item."
      redirect_to todo_list_todo_items_path
    else
      flash[:error] = "Something went wrong"
      render action: :new
    end
  end

  private
    def todo_item_params
      params[:todo_item].permit{:content}
    end
end

#index.html.erb

<h1><%= @todo_list.title %></h1>

<ul class="todo_items">
  <% @todo_list.todo_items.each do |todo_item| %>
    <li><%= todo_item.content %></li>
  <% end %>
</ul>
<p>
  <%= link_to "New Todo item", new_todo_list_todo_item_path %>
</p>

#new.html.erb

<%= form_for [@todo_list, @todo_item] do |form| %>
  <%= form.label :content %>
  <%= form.text_field :content %>

  <%= form.submit "Save" %>
<% end %>

当它出现在浏览器上时,它只显示列表符号而不显示内容。

抱歉太久了

将您的 todo_item_parems 更改为

def todo_item_params
  params.require(:todo_item).permit(:content)
end

看看Rails strong parameters.