469,326 Members | 1,548 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,326 developers. It's quick & easy.

Updating Individual database fields

3
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 9738
improvcornartist
303 Expert 100+
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
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 Expert 100+
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
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 Expert 100+
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

Post your reply

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

Similar topics

11 posts views Thread by Jason | last post: by
4 posts views Thread by Geoff | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by mdpf | last post: by
reply views Thread by listenups61195 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.