首页 > 解决方案 > 如何在 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 %>

标签: ruby-on-railsrubyruby-on-rails-4rubygems

解决方案


运行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#indexnew_task_pathtasks#new

请注意,某些路由需要一个:id,这是您要获取的记录的主键。对于show,和edit,您希望对特定记录执行这些操作。updatedestroy

因此,如果您使用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| %>

form_with


编辑:我刚刚注意到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 %>

form_for


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

推荐阅读