Javier Valencia
Javier Valencia

Reputation: 695

RoR edit and new form

I have two actions: new and edit, these call in view a partial named form.

new (view):

<h1>New Item</h1>
<%= render partial: 'form' %>

edit (view):

<h1>Edit Item</h1>
<%= render partial: 'form' %>

form (partial):

<%= form_for @item do |f| %>
<% f.field_for :foo %>
<% end %>

The model have a big particularity with id field in database, it's a compose column but in a single. I explain:

I have a items tuple:

id       | name              | foo
----------------------------------
10001000 | Name of item 1000 | bar
10001001 | Name of item 1001 | bar
10011000 | Name of item 1000 | bar
10021000 | Name of item 1000 | bar
10021001 | Name of item 1001 | bar

The first four digits of id column come from ficticious item_type_id column, and the next four digits are *item_id*. This f**k table can't be altered.

When run /items/new I dont have a problem, because the form generated its right action html property.

<form action="/items/new" method="post">...</form>

But, when I run /items/edit the form generated its like this:

<form action="/items/10021001/edit" method="post">...</form>

And its needed to be generated like this:

<form action="/items/1001/edit" method="post">...</form>

The first part of id column 1002 come from a session variable.

I'm using a trick from routes.rb

resources :items

I don't know if I be explained properly, I'm taking english lessons yet.

Upvotes: 0

Views: 82

Answers (1)

Thaha kp
Thaha kp

Reputation: 3709

Override to_param method in Item Model.

def to_param
  "#{id % 10000 }"
end

Upvotes: 1

Related Questions