473,471 Members | 1,778 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Updating Individual database fields

3 New Member
This is my form:

===========================
<% form_tag :action => 'update', :id => @book.id, :title => @book.title,
:price => @book.price, :description => @book.description %>
<%= render :partial => 'form' %>
<%= submit_tag 'Edit' %>
<% end %>

===========================

This is my controller code
===========================
def update

@book = Book.find(params[:id])

@book.update_attribute(:title, params[:title])
@book.update_attribute(:description, params[:description])
@book.update_attribute(:price, params[:price])


if @book.update_attributes(params[:book])



flash[:notice] = 'Book was successfully updated.'
redirect_to :action => 'show', :id => @book
else
render :action => 'edit'
end
end
=============================

I am trying to update only selected fields as shown in the database. Seems CRUD cant allow me. I am getting errors. How do I pass values from form fields to be used by the " @book.update_attribute". to update database fields; How can I update only selected using this method

fields in the database.

I ve struggled for a 2 weeks now. How do I go round it
Jan 10 '08 #1
5 10159
improvcornartist
303 Recognized Expert Contributor
Welcome to TSDN. In the future, please use code tags when posting code. They make your code look much prettier and easier to read. See the Posting Guidelines for helpful information.

What errors are you getting when you try to update the record? What does your form partial look like?
Jan 10 '08 #2
mpundu
3 New Member
This is my form partial.


================================================== ===
<%= error_messages_for 'book' %>

<!--[form:book]-->
<p><label for="book_title">Title</label><br/>
<%= text_field 'book', 'title' %></p>

<p><label for="book_price">Price</label><br/>
<%= text_field 'book', 'price' %></p>

<p><label for="book_description">Description</label><br/>
<%= text_area 'book', 'description' %></p>

<p><label for="book_created_at">Created at</label><br/>
<%= datetime_select 'book', 'created_at' %></p>

<p><label for="book_updated_at">Updated at</label><br/>
<%= datetime_select 'book', 'updated_at' %></p>
<!--[eoform:book]-->

================================================== ===
and this the error message i get when i click on edit

================================================== ===

Showing app/views/books/edit.rhtml where line #11 raised:
compile error
C:/railsapps/library/app/views/books/edit.rhtml:11: syntax error, unexpected kEND, expecting $end

Extracted source (around line #11):
8: <%= link_to 'Show', :action => 'show', :id => @book %> |
9: <%= link_to 'Back', :action => 'list' %>

Trace of template inclusion: /app/views/books/edit.rhtml

RAILS_ROOT: ./script/../config/..
================================================== ===
Jan 11 '08 #3
improvcornartist
303 Recognized Expert Contributor
To use code tags, click the # button when posting a message.

Instead of
Expand|Select|Wrap|Line Numbers
  1. @book.update_attribute(:title, params[:title])
have you tried
Expand|Select|Wrap|Line Numbers
  1. @book.update_attribute(:title, params[:book][:title])
It might just not be getting the correct param.

You may also need to change the
Expand|Select|Wrap|Line Numbers
  1. <% end %>
to
Expand|Select|Wrap|Line Numbers
  1. </form>
The error sounds like it doesn't like the 'end' tag.
Jan 11 '08 #4
mpundu
3 New Member
Hi . I made amendments as per your advice. Here is the controller and view code.

controller

Expand|Select|Wrap|Line Numbers
  1. def update
  2.  
  3.     @book = Book.find(params[:id])
  4.  
  5.     if @book.update_attributes(params[:book])
  6.  
  7.        @book.update_attributes(params[:book][:title][:price][:description])       
  8.        # when I remove " [:description]" above it works. 
  9.  
  10.         flash[:notice] = 'Book was successfully updated.'
  11.         redirect_to :action => 'show', :id => @book
  12.     else
  13.       render :action => 'edit'
  14.     end
  15.   end
  16.  
  17.   def destroy
  18.     Book.find(params[:id]).destroy
  19.     redirect_to :action => 'list'
  20.   end
Edit form

Expand|Select|Wrap|Line Numbers
  1. <h1>Editing book</h1>
  2.  
  3. <%= start_form_tag :action => 'update', :id => @book %>
  4.   <%= render :partial => 'form' %>
  5.   <%= submit_tag 'Edit' %>
  6. <%= end_form_tag %>
  7.  
  8. <%= link_to 'Show', :action => 'show', :id => @book %> |
  9. <%= link_to 'Back', :action => 'list' %>

form partial


Expand|Select|Wrap|Line Numbers
  1. <%= error_messages_for 'book' %>
  2.  
  3. <!--[form:book]-->
  4. <p><label for="book_title">Title</label><br/>
  5. <%= text_field 'book', 'title'  %></p>
  6.  
  7. <p><label for="book_price">Price</label><br/>
  8. <%= text_field 'book', 'price'  %></p>
  9.  
  10. <p><label for="book_description">Description</label><br/>
  11. <%= text_area 'book', 'description'  %></p>
  12.  
  13. <!--[eoform:book]-->


here is the error message I am getting

You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.[]

RAILS_ROOT: ./script/../config/..
Jan 14 '08 #5
improvcornartist
303 Recognized Expert Contributor
Try the following and see what it does.
Expand|Select|Wrap|Line Numbers
  1. #In def update, change
  2. @book.update_attributes(params[:book][:title][:price][:description])
  3. #to
  4. @book.update_attributes(params[:book][:title])
  5. @book.update_attributes(params[:book][:price])
  6. @book.update_attributes(params[:book][:description])
  7.  
Jan 14 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Jason | last post by:
Let's say I have an html form with 20 or 30 fields in it. The form submits the fields via POST to a php page which updates a table in a database with the $_POST vars. Which makes more sense? ...
1
by: Srinadh | last post by:
Hi all, We have files with about 20 to 30 fields per row. We are trying to update such files with about 60 rows as contiguous data in a CLOB field. It passes through. But when we try...
3
by: Tc | last post by:
Hi, I was curious, I am thinking of writing an application that loads a dataset from a database that resides on a server. The question I have is this, if multiple copies of the app will be...
0
by: cwbp17 | last post by:
I'm having trouble updating individual datagrid cells. Have two tables car_master (columns include Car_ID, YEAR,VEHICLE) and car_detail (columns include Car_ID,PRICE,MILEAGE,and BODY);both tables...
4
by: Geoff | last post by:
Hi I'm hoping somebody can help me with the following problem that has occurred to me. Suppose I have two tables in an SQL Server database. Let's call these tables A and B. Assume that A has...
11
by: bbasberg | last post by:
Hello, I have been struggling with this problem for DAYS and have googled my heart out as well as reading any books I could get my hands on but I cannot find any specific references to my problem....
1
by: robertrozarioa | last post by:
Hi , I am new to ACCESS.please help me out. I am designing a database.I have 3 tables, and have 3 forms based to collect data. 1. Individual ( fields are Name, Email, Status) Status is a...
0
by: oh4real | last post by:
I recently developed a compact function to efficiently allow users to change info in a form (like account info, contact info, etc.) and then the function automatically identifies what's changed and...
9
by: hrreece | last post by:
I have an Access 2002 database that has a form that can be used to review individual records. At the bottom of the form are buttons that are linked to functions that allow the user to "Find a record...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.