473,698 Members | 2,411 Online
Bytes | Software Development & Data Engineering Community
+ 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.descripti on %>
<%= render :partial => 'form' %>
<%= submit_tag 'Edit' %>
<% end %>

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

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

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

@book.update_at tribute(:title, params[:title])
@book.update_at tribute(:descri ption, params[:description])
@book.update_at tribute(:price, params[:price])


if @book.update_at tributes(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_at tribute". 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 10170
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_descr iption">Descrip tion</label><br/>
<%= text_area 'book', 'description' %></p>

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

<p><label for="book_updat ed_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
16195
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) simply UPDATING the values for all fields in the table, whether or not any particular field has actually changed 2) running a second SELECT statement and comparing the $_POST vars to the returned values, and only UPDATING those that have...
1
5953
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 updating files with about 60 to 200 rows, we get the
3
13064
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 running at once will there be problems with data updates? The reason I ask is I'm thinking like this: User1 launches the app and the dataset is created from the data in the DB.
0
1634
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 have a FK relationship on CAR_ID so the oracledataadapter1 select statement(CommandText) is: select car_master.car_id, car_master.year,car_master.vehicle,car_detail.car_id AS EXPR1,
4
2372
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 two fields: a primary key and another holding a string. In table B there are three fields: a primary key, a foreign key (which links to the primary key in A) and other field holding a string.
11
5767
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. I have been able to successfully loop through a directory reading spreadsheets that insert records into an Access database. The spreadsheets in said directory are all of the same format but have different values in the cells. It's when I turned...
1
1986
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 drop down list having values P,PE,E
0
2366
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 scans the database to find out where to change it. Basic concept is form is populated from an object from mysql tables, user changes fields (usually few - not for creating new accounts), $_POST is compared against an array/object with current field...
9
2524
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 using search criteria", "Delete the current record" and so on. After a user has used the search criteria to find a specific record, I would like to use the "delete" function on the form to not only delete the record, but also update another table. ...
0
8675
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
9160
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9029
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8897
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8862
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2331
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2002
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.