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
5 10159
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?
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/..
================================================== ===
To use code tags, click the # button when posting a message.
Instead of - @book.update_attribute(:title, params[:title])
have you tried - @book.update_attribute(:title, params[:book][:title])
It might just not be getting the correct param.
You may also need to change the
to
The error sounds like it doesn't like the 'end' tag.
Hi . I made amendments as per your advice. Here is the controller and view code. controller - def update
-
-
@book = Book.find(params[:id])
-
-
if @book.update_attributes(params[:book])
-
-
@book.update_attributes(params[:book][:title][:price][:description])
-
# when I remove " [:description]" above it works.
-
-
flash[:notice] = 'Book was successfully updated.'
-
redirect_to :action => 'show', :id => @book
-
else
-
render :action => 'edit'
-
end
-
end
-
-
def destroy
-
Book.find(params[:id]).destroy
-
redirect_to :action => 'list'
-
end
Edit form - <h1>Editing book</h1>
-
-
<%= start_form_tag :action => 'update', :id => @book %>
-
<%= render :partial => 'form' %>
-
<%= submit_tag 'Edit' %>
-
<%= end_form_tag %>
-
-
<%= link_to 'Show', :action => 'show', :id => @book %> |
-
<%= link_to 'Back', :action => 'list' %>
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>
-
-
<!--[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/..
Try the following and see what it does. -
#In def update, change
-
@book.update_attributes(params[:book][:title][:price][:description])
-
#to
-
@book.update_attributes(params[:book][:title])
-
@book.update_attributes(params[:book][:price])
-
@book.update_attributes(params[:book][:description])
-
Sign in to post your reply or Sign up for a free account.
Similar topics
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?
...
|
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...
|
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...
|
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...
|
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...
|
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....
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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,...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |