mercredi 31 mai 2017

Rails 3 Can't update record in DB

Hello I have model Types with 2 fields: photo and name. I can create new record but can't update it. Why? Operations is very similiar. Here's creating new record it's working. Controller

def new_type
 @type = Type.new(params[:type])
if @type.save
  redirect_to types_path
else
  render 'create_type'
end end

HTML file

<%= form_for @type, :url => { :action => "new_type" }, :html => { :multipart => true } do |f| %> <% end %>

<%= f.label :NAME %><br/>
<%= f.text_field :name %><br/>
<%= f.label :photo, "Your photo" %><br/>
<%= f.file_field :photo %><br />

<%= f.submit "ADD"%>  <% end %>

And Updating (not working)

def edit_type
 @type = Type.find(params[:id])
 end

def update_type
     @type = Type.find(params[:id])

if @type.update_attributes(params[:type])
  redirect_to types_path
else
  render 'edit_type'
end end

View with all my types

<% for t in @ts %>
<p> <%= t.name %></p>
<%= image_tag t.photo.url(:small) %><br/>
<%= link_to "EDIT", {:controller => "users", :action => "edit_type", :id => "#{t.id}" } %>

Edit VIEW

<%= form_for @type, :url => { :action => "edit_type", :id => "#{@type.id}" }, :html => { :multipart => true } do |f| %>

    <%= f.label :NAME %><br/>
    <%= f.text_field :name %><br/>
    <%= f.label :photo, "PHOTO" %><br/>
    <%= f.file_field :photo %><br />

    <%= f.submit "UPDATE"%>

<% end %>

Aucun commentaire:

Enregistrer un commentaire