【问题标题】:Rails Using Edit action in a Show ViewRails 在显示视图中使用编辑操作
【发布时间】:2013-05-19 21:16:14
【问题描述】:

我正在尝试更新其显示视图中的对象。我一直在关注 railscasts HABTM checkboxes

得到以下错误:

没有路由匹配 [PUT] "/accounts/4/edit"

这是我的表格:

<%= form_for @account, :url => { :action => "edit"} do |form| %>
    <%= hidden_field_tag "account[checklist_ids][]", nil%>
    <% Checklist.all.each do |checklist| %>
        <%= check_box_tag "account[checklist_ids][]", checklist.id, @account.checklist_ids.include?(checklist.id) %>
        <%= checklist.task %><br/>
    <% end %><br/>
<%= form.submit "Update Checklist", class: 'btn' %>
<% end %>

/app/controllers/accounts_controller.rb

class AccountsController < ApplicationController
  before_filter :authenticate_user!
  respond_to :html, :json

  def show
    @account = Account.find(params[:id])
    @notes = @account.notes.all
    @contacts = @account.contacts.all
  end


  def edit
    @account = Account.find(params[:id])
  end

  def update
    @account = Account.find(params[:id])
    @account.update_attributes(params[:account])
    respond_with @account
  end
end

/app/models/account.rb

class Account < ActiveRecord::Base
  attr_accessible :address, :city, :name, :phone, :state, :website, :zip, :contactname
  attr_accessible :conferences_attributes, :checklists_attributes
  has_many :notes, :dependent => :destroy 
  has_many :accountchecklists
  has_many :checklists, :through => :accountchecklists, :dependent => :destroy 
  has_many :contacts, :dependent => :destroy
  has_and_belongs_to_many :conferences
  accepts_nested_attributes_for :conferences
  accepts_nested_attributes_for :checklists
end

【问题讨论】:

    标签: ruby-on-rails forms


    【解决方案1】:

    &lt;%= form_for @account, :url =&gt; { :action =&gt; "edit"} do |form| %&gt;

    改成

    &lt;%= form_for(@account) do |form| %&gt;

    【讨论】:

      【解决方案2】:

      我变了

      <%= form_for @account, :url => { :action => "edit"} do |form| %>
      

      改成

      <%= form_for @account, :url => { :action => "update"} do |form| %>
      

      然后我让 checklist_ids 可以访问,因为我得到了一个无法批量分配的 checklist_ids

      class Account < ActiveRecord::Base
         attr_accessible  :checklist_ids
      end
      

      【讨论】:

        猜你喜欢
        • 2013-01-27
        • 2017-02-21
        • 2015-07-11
        • 1970-01-01
        • 2022-01-08
        • 1970-01-01
        • 2022-10-21
        • 1970-01-01
        • 2023-04-01
        相关资源
        最近更新 更多