【问题标题】:Create new table entry with user_id POST使用 user_id POST 创建新表条目
【发布时间】:2013-12-20 17:43:14
【问题描述】:

Ruby on Rails 3

我有一个提交 user_id 的表单。我正在尝试让 POST 使用 user_id 创建一个新的证书条目,并将 :attend 属性设置为“yes”。

发送的帖子是:

Parameters: {"utf8"=>"✓", "authenticity_token"=>"xYUdeMjkxa1b0AhNTKJi9sDbSPo9MbwMMVPrV7cgpyo=", "certificate"=>[{"user_id"=>"1"}], "commit"=>"Submit"}

这是我的编辑:

<%= form_for(@trained) do |f| %>
  <%= select_tag "certificate[][user_id]", options_for_select(@current_users.collect{|x| [x.name, x.id]}), {:multiple => :multiple} %>
  <%= f.submit "Submit", class: "btn btn-large btn-primary" %>
<% end %>

这是我的模型:

class Certificate < ActiveRecord::Base
  attr_accessible :attend, :pass, :user_id

  validates :user_id, presence: true
end

这是我的编辑控制器

def edit
@trained = Certificate.new(params[:user_id])
end

这是我用于 create() 的证书控制器

def create
  @trained = params[:certificate][:user_id] 
  params[:certificate][:attend] = "no"
  params[:certificate][:pass] = "no"
end

我一直在尝试不同的 create() 方法,但不知所措。如何获取 POST 以使用 user_id 创建新证书?我需要把 sumbit 按钮做成两个按钮,一个用于添加,一个用于删除。

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 forms input


    【解决方案1】:

    你为什么要 select_tag "certificate[][user_id]" 从那里删除多余的 []

    select_tag "certificate[user_id]"
    

    也在控制器中

    @trained = Certificate.new(params[:certificate])
    params[:certificate][:attend] = "no"
    params[:certificate][:pass] = "no"
    @trained.save
    

    【讨论】:

    • 你知道我为什么会收到这个错误吗:模板丢失缺少模板证书/创建,应用程序/创建与 {:locale=>[:en], :formats=>[:html], : handlers=>[:erb, :builder, :coffee]}。搜索:*“/opt/RoR/reseller_connect/app/views”
    • 是的,当您执行创建操作时,它将在该控制器的 views/ 文件夹中查找以 create 命名的文件,以便它可以向用户返回响应。所以如果你想渲染任何其他文件,你可以使用 render 'file_name' 或者如果你想将用户重定向到其他操作,而不是你可以使用 redirect_to 标签:)
    猜你喜欢
    • 2013-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-01
    • 2021-08-24
    • 2012-06-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多