1

I need a separate index/view from a scaffold generated table I’ve created. In this view, I have about half of the original scaffold columns, with the intention of a user being able to edit and update two of them. I created a non scaffold controller to generate this view. So, my questions:

1). Is it possible to combine scaffold and non scaffold resources in the non scaffold partial form?

2). Can I use the edit action and partial from the non scaffold controller with update going to the scaffold table?
I suppose in production, this would likely be some sort of authorization system?, but for now, I just want to find out the possibilities. My view/index in the non scaffold controller works, but of course the edit button does nothing. Am I missing something? For now, I’m using Rails 3.0 on Windows 7 if that makes any difference.

Routing in Partial:

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

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

This is my routes.rb:

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

This is the edit action in my non scaffold controller:

    def edit
    @achvr = Achvr.find(params[:id])
    end
KirDav
  • 11
  • 2
  • I haven't looked carefully, but I don't think there's ever a case where you want to render a partial as a stand-alone page. A partial is just supposed to be a kind of reusable view "function". Let Rails do the work of assembling the page (starting from the layout, and then the specific view and any of its partials, or theirs). – Tom Harrison Mar 22 '12 at 22:06

2 Answers2

0

I don't think your code works. since according to rails API (from rails2.3.x to 3.2), the "submit_tag" doesn't have the options your provided (

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

), what this tag supports are :

: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.

and also, I don't think your implement code make sense, a form could be submit only to 1 target (some_controller#some_action), however it seems that you want to submit a form to 2 actions at 1 time?

so, I think you had better write your code in Rails' MVC's way: combine the code you want to run into 1 action, e.g.:

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
Siwei
  • 19,858
  • 7
  • 75
  • 95
0

If I understand you right, you want to be able to, let's say, edit 2 columns of a record in 1 view.

Is the functionality like this?:

  1. You visit ravs#index
  2. ravs#index lists all the Achvr records
  3. You click edit on one of these records
  4. You are sent to ravs#edit with an :id matching the record
  5. In this view you can update the values of :col1 and :col2
  6. You hit the submit button which sends you to achvrs#update

Inside your view for ravs#edit you can have this. Using form_for like this, should work, since @achvr is an instance of the Achvr model. It would then point to its own controller.

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

In your achvrs#update action you can then check the params hash for its values. The scaffolded update method should however update things correctly. When you update from ravs#edit, params will only hold values for :col1 and :col2

A final word! This is not a proper way to do this. You might want to have a user model with permissions. Then filter which values can be edited based on those.

martinjlowm
  • 871
  • 5
  • 8
  • Thanks so much for your reply. Yes, you have my intentions correct, and I figured that in a production environment this would be based on a model and permissions. Will look for and post on that in time. Thanks again. – KirDav Mar 26 '12 at 15:32