Button invokes "update" instead of "index"

45 views
Skip to first unread message

Ronald Fischer

unread,
Jun 18, 2014, 7:31:03 AM6/18/14
to rubyonra...@googlegroups.com
I have nested resources

resources :dicts do
resources :cards
end

and in my form

form_for [@dict,@card] do |f|

I have the following button:

<%= f.submit('LIST ALL', url: dict_cards_path(@dict.id),
class:'kanren_button', name:'list_all', method: :get) %>

The generated HTML code is:

<input class="kanren_button" method="get" name="back_to_list"
type="submit" url="/dicts/6/cards" value="BACK TO LIST" />

I would expect that clicking this button would call Card.index, but I
get instead the error message

The action 'update' could not be found for CardsController

It is correct that I don't have a CardsController.update yet, but I
don't think I should need one at this point.

The output from 'rake routes|grep card' looks fine for me:

dict_cards GET /dicts/:dict_id/cards(.:format)
cards#index
POST /dicts/:dict_id/cards(.:format)
cards#create
new_dict_card GET /dicts/:dict_id/cards/new(.:format) cards#new
edit_dict_card GET /dicts/:dict_id/cards/:id/edit(.:format)
cards#edit
dict_card GET /dicts/:dict_id/cards/:id(.:format)
cards#show
PATCH /dicts/:dict_id/cards/:id(.:format)
cards#update
PUT /dicts/:dict_id/cards/:id(.:format)
cards#update
DELETE /dicts/:dict_id/cards/:id(.:format)
cards#destroy

What did I do wrong?

--
Posted via http://www.ruby-forum.com/.

mike2r

unread,
Jun 18, 2014, 7:08:02 PM6/18/14
to rubyonra...@googlegroups.com
The method and url parameters you have in the submit helper that show up in the output are going to be ignored, they aren't valid parameters for submit.  Those items are actually defined above in the html form tag (which is generated from your form_for statement).  My guess is that if you look at the log, this is coming across with an action of PATCH or PUT.  


Ronald Fischer

unread,
Jun 19, 2014, 2:10:24 AM6/19/14
to rubyonra...@googlegroups.com
Does this mean that I can't solve this with a *button*, but would have
to use a *link* instead?

Walter Lee Davis

unread,
Jun 19, 2014, 7:12:35 AM6/19/14
to rubyonra...@googlegroups.com

On Jun 19, 2014, at 2:09 AM, Ronald Fischer wrote:

> Does this mean that I can't solve this with a *button*, but would have
> to use a *link* instead?

It's important to note that you may style a link to look like anything -- even a button. But you cannot remove the button-ness from an actual button and have it do anything besides submit the form containing it.

Walter

>
> --
> Posted via http://www.ruby-forum.com/.
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/b7071f13fb2a2efa9fcd84a80eb3ffb6%40ruby-forum.com.
> For more options, visit https://groups.google.com/d/optout.

Ronald Fischer

unread,
Jun 19, 2014, 7:29:38 AM6/19/14
to rubyonra...@googlegroups.com
Walter Davis wrote in post #1150186:
> On Jun 19, 2014, at 2:09 AM, Ronald Fischer wrote:
>
>> Does this mean that I can't solve this with a *button*, but would have
>> to use a *link* instead?
>
> It's important to note that you may style a link to look like anything
> -- even a button. But you cannot remove the button-ness from an actual
> button and have it do anything besides submit the form containing it.

I see. Maybe I'm using the wrong design here in the first place. Here is
what I am going to do:

The user arrives the web page in question, from an overview page. The
overview page gives a list of all instances (in this case, of type
Card), by listing only the primary key of the instance. When the user
clicks on one of those keys, s/he arrives at a page displaying all the
data for this Card instance.

At this point, I will give the user three choices:

- delete the Card
- edit the Card
- show again the "list of all cards"

My (probably stupid) idea was to write a "form" without input fields,
showing only the information on the cards, and having 3 buttons for
DELETE, EDIT and LIST.

Now I understand that a submit button in a form helper for a form_for,
which has as argument an existing object, automatically calls the
"update" function. Thinking about it, this makes sense, and I now think
that I was misusing the form_for().

Maybe I should just output the data without using a form at all, and use
button_to() if I want the clickable part look or behave like buttons, or
use links and style them as buttons, as you pointed out. In any case,
using a form in my particular case, doesn't make much sense. Would you
agree on that?

Matt Jones

unread,
Jun 19, 2014, 7:31:47 AM6/19/14
to rubyonra...@googlegroups.com


On Wednesday, 18 June 2014 06:31:03 UTC-5, Ruby-Forum.com User wrote:
I have nested resources

  resources :dicts do
    resources :cards
  end

and in my form

  form_for [@dict,@card] do |f|

I have the following button:

  <%= f.submit('LIST ALL', url: dict_cards_path(@dict.id),
class:'kanren_button', name:'list_all', method: :get) %>

The generated HTML code is:

   <input class="kanren_button" method="get" name="back_to_list"
type="submit" url="/dicts/6/cards" value="BACK TO LIST" />


URL + method are properties of the "form" tag, not of the submit button. This will not work.

A better question: what is this button supposed to *do*? It appears to be on a form that's *editing* a particular card...

--Matt Jones

Walter Lee Davis

unread,
Jun 19, 2014, 8:07:12 AM6/19/14
to rubyonra...@googlegroups.com
I think you would get a lot of mileage out of reading the REST article in Rails Guides. Particularly try to absorb how Rails does its own flavor of REST routing. To delete an object, you send a DELETE (header) request, which Rails simulates as a POST with a method: '_delete' hidden field, since most browsers don't properly support that request type. If you were to hand-make a form with such a method within it, wrapped around your delete button, then it would just work. But if you put that button inside a regular form_for @your_object, then the form will have either the POST (new object) or PUT/PATCH (existing object being updated) path and header, and the delete will fail.

Walter


> --
> Posted via http://www.ruby-forum.com/.
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/a1e40d17786e980f2fba3705ab8aade8%40ruby-forum.com.
Reply all
Reply to author
Forward
0 new messages