1

我需要一个来自我创建的脚手架生成表的单独索引/视图。在这个视图中,我有大约一半的原始脚手架列,目的是让用户能够编辑和更新其中的两个。我创建了一个非脚手架控制器来生成这个视图。所以,我的问题:

1)。是否可以以非脚手架部分形式组合脚手架和非脚手架资源?

2)。我可以使用非脚手架控制器的编辑操作和部分更新到脚手架表吗?
我想在生产中,这可能是某种授权系统?但现在,我只想找出可能性。我在非脚手架控制器中的视图/索引有效,但编辑按钮当然什么也不做。我错过了什么吗?现在,我在 Windows 7 上使用 Rails 3.0,如果这有什么不同的话。

部分路由:

<%= form_tag(:controller=> "ravs", :method=> "get", :action=> "edit", :class=>    "_dec") %>

    <div >
    <%= submit_tag(:controller=> "achvrs", :method=> "put", :action=> "update") %>
    </div>
    <% end %>

这是我的路线.rb:

    Effcnt::Application.routes.draw do
    get "ravs/index"
    get "ravs/edit"
    get "ravs/_dec"
    resources :achvrs

这是我的非脚手架控制器中的编辑操作:

    def edit
    @achvr = Achvr.find(params[:id])
    end
4

2 回答 2

0

我不认为你的代码有效。因为根据 rails API(从 rails2.3.x 到 3.2),“submit_tag”没有您提供的选项(

<%= submit_tag(:controller=> "achvrs", :method=> "put", :action=> "update") %>

),这个标签支持的是:

:confirm => 'question?' - This will add a JavaScript confirm prompt with the question specified. If the user accepts, the form is processed normally, otherwise no action is taken.
:disabled - If true, the user will not be able to use this input.
:disable_with - Value of this parameter will be used as the value for a disabled version of the submit button when the form is submitted.
Any other key creates standard HTML options for the tag.

而且,我认为您的实现代码没有意义,一个表单只能提交给 1 个目标(some_controller#some_action),但是您似乎想一次将表单提交给 2 个操作?

所以,我认为你最好以 Rails 的 MVC 方式编写代码:将你想要运行的代码组合成 1 个动作,例如:

def the_action_your_form_submitted_to 
    # previously it was called in your RESTful action 
    do_task_no.1   
    # previously it was called in your non-RESTful action
    do_task_no.2   
end
于 2012-03-22T23:22:48.530 回答
0

如果我理解正确,您希望能够在 1 个视图中编辑记录的 2 列。

功能是这样的吗?:

  1. 你访问ravs#index
  2. ravs#index列出所有 Achvr 记录
  3. 您在这些记录之一上单击编辑
  4. 您被发送到ravs#edit匹配:id的记录
  5. 在此视图中,您可以更新:col1:col2
  6. 您点击提交按钮,将您发送到achvrs#update

在您的视图中,ravs#edit您可以拥有这个。像这样使用form_for应该可以工作,因为@achvr它是Achvr模型的一个实例。然后它将指向它自己的控制器。

    <%= form_for(@achvr, :html => {:class => '_dec'}) do |f| %>
      <%= f.text_field :col1 %>
      <%= f.text_field :col2 %>
      <%= f.submit %>
    <% end %>

然后,在您的achvrs#update操作中,您可以检查params哈希的值。然而,脚手架更新方法应该正确更新内容。当您从 更新时ravs#editparams将仅保留:col1和的值:col2

最后一句话!这不是执行此操作的正确方法。您可能希望拥有一个具有权限的用户模型。然后根据这些过滤哪些值可以编辑。

于 2012-03-22T23:59:33.233 回答