Dodjs
Dodjs

Reputation: 819

Ruby update action not working

I have one model: new and its controller: news

The news controller methods:

    def edit
    @new = New.find(params[:id].to_i)
  end

  def update
    @new = New.find(params[:id].to_i)
    @new.update_attributes(params[:new])
    flash[:notice] = "Sikeresen frissítve"
    redirect_to news_path
  rescue ActiveRecord::RecordInvalid
    flash[:error] = "Valami hiba lépett fel"
    redirect_to edit_news_path(@new)
  end

And here is the edit.html.erb

<%= error_messages_for :news %>

<% form_for :new, :url=>news_path(@new), :html=>{:method=>:put}  do |m|%>

  <p>  <%= m.label(:title,"title: ") %>

  <%= m.text_field(:title) %></p>

  <p>

    <%= m.label(:text, "text: ") %>

    <%= m.text_area(:text) %>

  </p>

    <%= submit_tag "Save" %>

<% end %>

It's works fine, no error, the field showed up in the textboxes, but if I click the submit button I gave unknown action error

Upvotes: 0

Views: 169

Answers (2)

Max Williams
Max Williams

Reputation: 32933

I think it should be

:url=>new_path(@new)

not

:url=>news_path(@new)

though i'm confused just thinking about it due to you calling your model "new".

I STRONGLY recommend you change your model name (and controller and views) to avoid confusion. "New" is used in rails in the context of the 'new' action. If you don't change it you will have things like "new_new_path" and "update_new_path" which is going to make you cry at some point.

Also, it doesn't really make sense to talk about 'a new'. The singular of 'news' isn't 'new'. It's very confusing all round, sort it out now before you build any more stuff around it.

I'd recommend "Article" or something similar as a model name.

Upvotes: 3

bjg
bjg

Reputation: 7477

Try this:

form_for :new, :url=>{ :action => "update" }, :html=>{:method=>:put}  do |m|

Upvotes: 0

Related Questions