ruby-on-rails - 如何在 ruby on rails 中发布数据
问题描述
我是 ruby on rails 的新手。我正在尝试为登录的用户添加任务,但是当我点击任务/新路径时,如果我从表单中删除 url,它会在表单 url 中显示 new.html.erb 文件中的错误,但在提交表单后数据未保存在数据库中
No route matches {:action=>"show", :controller=>"task", :id=>nil}, missing required keys: [:id]
任务控制器
class TaskController < ApplicationController
def get; end
def index
@task = Task.all
end
def show
@task = Task.find(params[:id])
end
def new
@task = Task.new
end
def create
@task = Task.new(params.permit(:tasks).permit(:daily_task, :date, :current_user.id))
respond_to do |format|
if @task.save
format.html { redirect_to @task, notice: 'Task added' }
else
format.html { render :new }
end
end
end
架构
create_table "tasks", force: :cascade do |t|
t.string "daily_task"
t.datetime "date"
t.bigint "users_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["users_id"], name: "index_tasks_on_users_id"
end
新的.html.erb
<h3>add task</h3>
<%= form_for :task, url: task_path do |f| %>
Add task: <%=f.text_field :daily_task %><br>
date: <%=f.datetime_select :date %>
<%= f.submit "Add" %>
<% end %>
解决方案
运行rails routes
将向您显示应用程序中的可用路由:
$ rails routes | grep task
tasks GET /tasks(.:format) tasks#index
POST /tasks(.:format) tasks#create
new_task GET /tasks/new(.:format) tasks#new
edit_task GET /tasks/:id/edit(.:format) tasks#edit
task GET /tasks/:id(.:format) tasks#show
PATCH /tasks/:id(.:format) tasks#update
PUT /tasks/:id(.:format) tasks#update
DELETE /tasks/:id(.:format) tasks#destroy
您可以在第一列_path
中为字符串添加后缀,以创建可用的路由。例如,tasks_path
将重定向到tasks#index
和new_task_path
到tasks#new
。
请注意,某些路由需要一个:id
,这是您要获取的记录的主键。对于show
,和edit
,您希望对特定记录执行这些操作。update
destroy
因此,如果您使用task_path
,则涉及您要获取特定记录;这是不正确的。相反,您需要指向tasks#create
(POST 请求)。您可以通过以下几种方式进行操作:
# Considering `@task` is your object. You might be using `task` if you're using partials
<%= form_with model: @task do |f| %>
<%= form_with model: @task, url: tasks_path do |f| %>
<%= form_with model: @task, url: tasks_path, method: :post do |f| %>
<%= form with model: Task.new do |f| %>
编辑:我刚刚注意到ruby-on-rails-4
标签,form_with
您将无法使用。
完全一样:
<%= form_for @task do |f| %>
<%= form_for @task, url: tasks_path do |f| %>
<%= form_for @task, url: tasks_path, method: :post do |f| %>
<%= form_for Task.new %>
EDIT2:关于undefined method 'id' for :current_user:Symbol
您正在调用.id
. Symbol
另外,您可能希望在私有方法中实现强参数。
未经测试的代码,但我会做类似的事情:
#/app/controllers/tasks_controller.rb
class TasksController < ApplicationController
def create
# set user_id in the backend, since POST requests can easily be modified
parameters = task_params.merge({ user_id: current_user.id })
@task = Task.new(parameters)
respond_to do |format|
if @task.save
format.html # ...
else
format.html # ...
end
end
private
def task_params
params.require(:task).permit(:daily_task, :date, :user_id) # `user_id` without s
end
end
推荐阅读
- cmake - CMake set_property() 似乎不适用于源文件
- javascript - PinchZoom.js 与 iOS 设备上的 Owl Carousel 不兼容
- c# - jqGrid 树网格版本 4.5.2 无法让树工作
- angular - MAT_DATEPICKER_VALIDATORS 的错误代码是什么?
- sql-server - Docker 上 SQLserver 中 FILESTREAM 功能的替代方案
- javascript - Fabric.js loadFromJSON 将对象分配给未正确转换的自定义属性
- postgresql - PostgreSQL 将 bytea 数据直接转储到文件
- c++ - 缓冲区溢出?
- java - “如何使用 Android Studio 修复这个 post HTTP 请求?”
- laravel-5 - Cpanel 上 phpunit 文件夹中的恶意软件