473,626 Members | 3,322 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 10167
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
16153
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
5946
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
13044
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
1629
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
2370
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
5763
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
1984
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
2363
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
2518
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
8265
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
8196
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8705
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
7193
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5574
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
4197
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2625
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 we have to send another system
1
1808
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1511
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.