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
5 10167
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_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/..
=============== =============== =============== ========
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?
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...
|
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
|
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.
|
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,
|
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.
| |
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...
|
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
|
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...
|
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. ...
|
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...
|
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,...
| |
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...
|
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...
|
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();...
|
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 we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |