0

I have a model called sample. To simplify, "sample" will have a "name", "price" and "approved" fields.

I want to have a page that will list all samples that have "approved = 'false'". In this page I want to be able to edit the sample fields, check the approved checkbox when done, and then press the form button to approve all the selected sample fields.

I watched railscast #198 but it has a previous page that I want to avoid... He has a webpage listing all samples, and then check the ones he wants to edit and then he finally has access to the "edit individual" page. I want to skip this page and go directly to the approve page.

On my sample controller I've got:

def edit_individual
   @samplestoapprove = Sample.where(:approve => 'false')
end

On my routes.rb I have:

resources :samples, :collection => { :edit_individual => :post, :update_individual => :put }

If we ignore for now the "update_individual" code on the controller,I then created a view inside samples, just to check if I could list the unapproved samples. I called it "edit_individual.html.erb"

<% title "Edit Samples" %>
<%= form_tag update_individual_samples_path :method => :put do  %>
<%= for sample in @samplestoapprove %>
    <%= fields_for "samplestoapprove[]", sample do |f| %>
        <h2><%= f sample.id %></h2>
    <%= end %>
<%= end %>
<p><%= submit_tag "Approve" %></p>
<%= end %>

I then tried to access "localhost:3000/samples/edit_individual" but I get an error "Couldn't find Sample with id=edit_individual"

Can anyone help me? I want to have an option on the menu called "Approve" with a link to this "edit_individual" that when I click it, it shows me all samples to be approved with the form mentioned before... Is this easy to achieve? I'm a going on the right path?

Or should I go for something like a datagrid? Like for example http://www.tutorialized.com/tutorial/Editable-Datagrid-for-Ruby-on-Rails-Built-with-dhtmlxGrid/60309

Here's the code on my routes.rb related to this issue:

resources :samples, :collection => { :get => :edit_individual,
  :update_individual => :put}
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
lbramos
  • 327
  • 1
  • 6
  • 19

1 Answers1

1

In your routes you have :edit_individual => :post, but when you visit a URL in your browser like you did with http://localhost:3000/samples/edit_individual, that is doing a GET request.

I'm guessing if you look in the logs where your Rails server is running, you'll see that your request when you visit that URL is going to your SampleController#show action and trying to use edit_individual as the ID of one Sample that should be shown.

I would recommend changing your routes to this as shown in the Rails routing guide section 2.10.2:

resources :samples do
  collection do
    get 'edit_individual'
  end
end
carols10cents
  • 6,943
  • 7
  • 39
  • 56
  • Hi, thanks for your reply. You're right on the GET, it0s going to the SampleController#show. But unfortunately your code doesn't work, I get an error on that "end" and the "collection do"... – lbramos Sep 25 '13 at 23:19
  • Could you post what your `routes.rb` has now please? – carols10cents Sep 25 '13 at 23:30
  • Oh, I was wondering what your routes.rb had when you added my code... I'm not sure why you're getting that error because I don't when I use the code I have there. I'm wondering if it's because you have curly braces in your routes and my code uses `do... end`. I think it's an interaction between the code in your routes.rb and the code I gave you but it's hard to say what the problem is without seeing all the code. – carols10cents Sep 28 '13 at 14:17
  • Oh wait, I see you have edited my code to fit your style. So what isn't working now? – carols10cents Sep 28 '13 at 14:19
  • Actually, I thought you were using curly braces for block syntax but you're using them for Hashes. I'm not sure if your syntax is valid like that-- I've never done routes that way with hash keys and values. I don't see anything in the documentation about being able to create nested routes like that -- you might want to take a look at [the rails routing guide](http://guides.rubyonrails.org/routing.html). – carols10cents Sep 28 '13 at 14:25
  • Hi @carols10cents, I ended up using DHTMLX DataGrid but I used your tips on my routes file anyway, so I'm marking yours as the best answer! – lbramos Apr 16 '14 at 10:54