- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我在 rails 中有一个嵌套的路由/模型/表单。在我的索引页面上,我列出了 todo_lists,下面是 todo_items。我希望能够单击我的待办事项列表标题,然后它会将我带到编辑页面。我研究多态路由和嵌套路由。
更新
这是我阻止它创建虚拟待办事项列表的修复程序。
<% @current_todo_lists.each do |list| %>
<% if list.id %>
<div class="panel">
<p><strong><%= link_to list.title ,edit_todo_list_path(list)%></strong></p>
<% list.todo_items.each do |todo_item| %>
<p><%= todo_item.description %></p>
<% end %>
</div>
<% end %>
<% end %>
Link_to rails nested form edit
polymorphic_path not generating correct path我已经做了很多相当多的研究来寻找茧,多态路线上的导轨指南和其他几个 stackoverflow 链接。
我没有成功完成这些工作。
这是索引页面,其中列出了我所有的 todo_lists 和 todo_items。它通过一个循环来列出每个待办事项列表及其创建的相应项目
更新:
我已经试过了 <%= link_to list.title, edit_todo_list_path(list) %>
和 <%= link_to list.title, edit_todo_list_path(@list) %>
.我收到的错误消息是:
ActionController::UrlGenerationError at /todo_lists
与 @todo_list 相同的配置给出了相同的错误。基本上它找不到带有 id 的待办事项列表。
No route matches {:action=>"edit", :controller=>"todo_lists", :id=>nil} missing required keys: [:id]
在控制台中确实给了我一个结果。所以我错过了一些东西。
>> t = TodoList.find(1)
=> #<TodoList id: 1, title: "First todo List with a modal", created_at: "2014-09-09 23:02:27", updated_at: "2014-09-09 23:02:27", user_id: 1>
>>
更新 2: 这是我的待办事项列表 Controller 中发生错误的地方。没有id是找不到的。
def set_todo_list
@todo_list = TodoList.find(params[:id])
end
<% @todo_lists.each do |list| %>
<p><strong><%= link_to list.title, edit_polymorphic_path(@todo_list) %></strong></p>
<% list.todo_items.each do |todo_item| %>
<p><%= todo_item.description %></p>
<% end %>
<% end %>
到目前为止 <p><strong><%= link_to list.title, edit_polymorphic_path(@todo_list) %
参数有@todo_list(s)、@todo_lists(s)、todo_items等。
模型:
class TodoList < ActiveRecord::Base
has_many :todo_items, dependent: :destroy
accepts_nested_attributes_for :todo_items, allow_destroy: true
validates_presence_of :title
end
class TodoItem < ActiveRecord::Base
belongs_to :todo_list
end
Controller :
Class TodoListsController < ApplicationController
before_filter :authenticate_user!
before_filter except: [:index]
before_action :set_todo_list, only: [:show, :edit, :update, :destroy]
# GET /todo_lists
# GET /todo_lists.json
def index
#@todo_lists = TodoList.all
#find current user todo lists/items
@todo_lists = current_user.todo_lists
@todo_items = current_user.todo_items
#create a new user todo list
@todo_list = current_user.todo_lists.new
# builder for todo list _form
3.times{ @todo_list.todo_items.build }
end
# GET /todo_lists/1
# GET /todo_lists/1.json
def show
end
# # GET /todo_lists/new
def new
@todo_list = current_user.todo_lists.new
3.times{ @todo_list.todo_items.build }
end
# GET /todo_lists/1/edit
def edit
#@todo_list = TodoList.find(todo_list_params)
@todo_list = TodoList.find(params[:id])
end
# POST /todo_lists
# POST /todo_lists.json
def create
#@todo_list = TodoList.new(todo_list_params)
@todo_list = current_user.todo_lists.new(todo_list_params)
respond_to do |format|
if @todo_list.save
format.html { redirect_to @todo_list, notice: 'Todo list was successfully created.' }
format.json { render :show, status: :created, location: @todo_list }
else
format.html { render :new }
format.json { render json: @todo_list.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /todo_lists/1
# PATCH/PUT /todo_lists/1.json
def update
@todo_list = TodoList.find(params[:id])
respond_to do |format|
if @todo_list.update(todo_list_params)
format.html { redirect_to @todo_list, notice: 'Todo list was successfully updated.' }
format.json { render :show, status: :ok, location: @todo_list }
else
format.html { render :edit }
format.json { render json: @todo_list.errors, status: :unprocessable_entity }
end
end
end
# DELETE /todo_lists/1
# DELETE /todo_lists/1.json
def destroy
#@todo_list.TodoList.find(params[:id])
@todo_list.destroy
respond_to do |format|
format.html { redirect_to todo_lists_url, notice: 'Todo list was successfully destroyed.' }
format.json { head :no_content }
end
end
private
def owns_todolist
if current_user != TodoList.find(params[:id]).user
redirect_to todo_lists_path, error: "You can't do that!"
end
end
# Use callbacks to share common setup or constraints between actions.
def set_todo_list
@todo_list = TodoList.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def todo_list_params
params.require(:todo_list).permit(:title, todo_items_attributes: [:description, :_destroy])
end
end
class TodoItemsController < ApplicationController
before_action :set_todo_item, only: [:show, :edit, :update, :destroy]
before_action :set_todo_list
# GET /todo_items
# GET /todo_items.json
def index
@todo_items = TodoItem.all
end
# GET /todo_items/1
# GET /todo_items/1.json
def show
@todo_item = TodoItem.find(params[:id])
end
# GET /todo_items/new
def new
@todo_item = @todo_list.todo_items.build
end
# GET /todo_items/1/edit
def edit
@todo_item = TodoItem.find(params[:id])
end
# POST /todo_items
# POST /todo_items.json
def create
@todo_item = @todo_list.todo_items.build(todo_item_params)
respond_to do |format|
if @todo_item.save
format.html { redirect_to [@todo_list,@todo_item], notice: 'Todo item was successfully created.' }
format.json { render :show, status: :created, location: @todo_item }
else
format.html { render :new }
format.json { render json: @todo_item.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /todo_items/1
# PATCH/PUT /todo_items/1.json
def update
@todo_item = TodoItem.find(params[:id])
respond_to do |format|
if @todo_item.update(todo_item_params)
format.html { redirect_to @todo_item, notice: 'Todo item was successfully updated.' }
format.json { render :show, status: :ok, location: @todo_item }
else
format.html { render :edit }
format.json { render json: @todo_item.errors, status: :unprocessable_entity }
end
end
end
# DELETE /todo_items/1
# DELETE /todo_items/1.json
def destroy
@todo_item = TodoItem.find(params[:id])
@todo_item.destroy
respond_to do |format|
format.html { redirect_to todo_list_todo_items_url, notice: 'Todo item was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_todo_item
@todo_item = TodoItem.find(params[:id])
end
def set_todo_list
@todo_list = TodoList.find(params[:todo_list_id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def todo_item_params
params.require(:todo_item).permit(:description, :text, :todo_list_id)
end
end
最后是表单。现在它允许添加一个 todo_list 和一些 todo_items 就像练习一样。我计划使用一些 Ajax 以允许稍后进行动态创建。并具有不同的编辑形式。
<%= form_for(@todo_list) do |f| %>
<% if @todo_list.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@todo_list.errors.count, "error") %> prohibited this todo_list from being saved:</h2>
<ul>
<% @todo_list.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :title %><br>
<%= f.text_field :title %>
</div>
<div>
<%= f.fields_for :todo_items do |builder| %>
<%= builder.label :description, "Items" %>
<%= builder.text_field :description %>
<%= builder.check_box '_destroy' %>
<% end %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
最佳答案
我认为这个问题实际上有点棘手,因为 edit_todo_list_path(list)
似乎会抛出同样的错误。
发生的事情是,当您运行 @todo_list = current_user.todo_lists.new
时,@todo_lists
变量(首先是一个持久列表数组)发生了变化。该命令实际上将一个新的(未保留的)列表添加到 @todo_lists
数组的末尾(对我来说似乎是错误的行为,但以前发生在我身上),因此当您的 View 循环遍历它们时,最后一个没有id,无法为其创建路径。
解决方案是(我认为)在使用 @todo_lists
变量后创建该变量。
从 Controller 中取出 @todo_list
,在 View 中使用它的地方,改为执行 current_user.todo_lists.build
。这应该实例化一个新列表而不改变@todo_lists
变量。
关于ruby - Rails 4 嵌套表单 link_to 编辑不在循环中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25756078/
以下是一个非常简单的ruby服务器。 require 'socket' local_socket = Socket.new(:INET, :STREAM) local_addr = Socket.
我正在使用 OS X(使用 bash),并且是 unix 的新手。我想知道是否可以修改一些文件以便运行 ruby 程序,我不需要“ruby file.rb”,而是可以运行“ruby.rb”。 有理
我在用 Ruby 替换字符串时遇到一些问题。 我的原文:人之所为不如兽之所为。 我想替换为:==What== human does is not like ==what== animal does.
我想在一个循环中从 Ruby 脚本做这样的事情: 写一个文件a.rb(每次迭代都会改变) 执行系统(ruby 'a.rb') a.rb 将带有结果的字符串写入文件“results” a.rb 完成并且
我的问题是尝试创建一个本地服务器,以便我可以理解由我的新团队开发的应用程序。我的问题是我使用的是 Ruby 2.3.3,而 Gemfile 需要 2.3.1。我无法编辑 Gemfile,因为我被告知很
我有一个使用 GLI 框架用 Ruby 编写的命令行实用程序。我想在我的主目录中配置我的命令行实用程序,使用 Ruby 本身作为 DSL 来处理它(类似于 Gemfile 或 Rakefile)。 我
我的 Rails 应用 Controller 中有这段代码: def delete object = model.datamapper_class.first(:sourced_id =>
我正在寻找的解析器应该: 对 Ruby 解析友好, 规则设计优雅, 产生用户友好的解析错误, 用户文档的数量应该比计算器示例多, UPD:允许在编写语法时省略可选的空格。 快速解析不是一个重要的特性。
我刚开始使用 Ruby,听说有一种“Ruby 方式”编码。除了 Ruby on Rails 之外,还有哪些项目适合学习并被认可且设计良好? 最佳答案 Prawn被明确地创建为不仅是一个该死的好 PDF
我知道之前有人问过类似的问题,但是我该如何构建一个无需在前面输入“ruby”就可以在终端中运行的 Ruby 文件呢? 这里的最终目标是创建一个命令行工具包类型的东西。现在,为了执行我希望用户能够执行的
例如哈希a是{:name=>'mike',:age=>27,:gender=>'male'}哈希 b 是 {:name=>'mike'} 我想知道是否有更好的方法来判断 b 哈希是否在 a 哈希内,而
我是一名决定学习 Ruby 和 Ruby on Rails 的 ASP.NET MVC 开发人员。我已经有所了解并在 RoR 上创建了一个网站。在 ASP.NET MVC 上开发,我一直使用三层架构:
最近我看到 Gary Bernhardt 展示了他用来在 vim 中执行 Ruby 代码的 vim 快捷方式。捷径是 :map ,t :w\|:!ruby %. 似乎这个方法总是执行系统 Rub
在为 this question about Blue Ruby 选择的答案中,查克说: All of the current Ruby implementations are compiled to
我有一个 Ruby 数组 > list = Request.find_all_by_artist("Metallica").map(&:song) => ["Nothing else Matters"
我在四舍五入时遇到问题。我有一个 float ,我想将其四舍五入到小数点后的百分之一。但是,我只能使用 .round ,它基本上将它变成一个 int,意思是 2.34.round # => 2. 有没
我使用 ruby on rails 编写了一个小型 Web 应用程序,它的主要目的是上传、存储和显示来自 xml(文件最多几 MB)文件的结果。运行大约 2 个月后,我注意到 mongrel 进程
我们如何用 Ruby 转换像这样的字符串: 𝑙𝑎𝑡𝑜𝑟𝑟𝑒 收件人: Latorre 最佳答案 s = "𝑙𝑎𝑡𝑜𝑟𝑟𝑒" => "𝑙𝑎𝑡𝑜𝑟𝑟𝑒" s.u
通过 ruby monk 时,他们偶尔会从左侧字段中抛出一段语法不熟悉的代码: def compute(xyz) return nil unless xyz xyz.map {|a,
不确定我做错了什么,但我似乎弄错了。 问题是,给你一串空格分隔的数字,你必须返回最大和最小的数字。 注意:所有数字都是有效的 Int32,不需要验证它们。输入字符串中始终至少有一个数字。输出字符串必须
我是一名优秀的程序员,十分优秀!