mysql - Unable to perform edit operation in ruby on rails -


i new bee ruby on rails, have created small project add,update , delete record mysql db

i able add , delete record mysql db ruby application

but issue when try update existing record

my code follows,

controller:

class bookcontroller < applicationcontroller def list       @books = book.find(:all)    end    def show       @book = book.find(params[:id])    end    def new       @book = book.new       @subjects = subject.find(:all)    end    def create       @book = book.new(params[:book])       if @book.save             redirect_to :action => 'list'       else             @subjects = subject.find(:all)             render :action => 'new'       end    end    def edit       @book = book.find(:all)        @subjects = subject.find(:all)    end    def update       @book = book.find(params[:id])       if @book.update_attributes(params[:book])          redirect_to :action => 'show', :id => @book       else          @subjects = subject.find(:all)          render :action => 'edit'       end    end    def delete       book.find(params[:id]).destroy       redirect_to :action => 'list'    end    def show_subjects       @subject = subject.find(params[:id])    end end 

list html:

<% if @books.blank? %> <p>there not books in system.</p> <% else %> <p>these current books in our system</p> <ul id="books"> <% @books.each |c| %> <li> <%= link_to c.title, {:action => 'show', :id => c.id} -%>  <b><%= link_to "edit",  {:action => 'edit', :id => c.id} %></b>  <b> <%= link_to "delete", {:action => 'delete', :id => c.id}, :confirm => "are sure want delete item?" %></b> </li> <% end %> </ul> <% end %> <p><%= link_to "add new book", {:action => 'new' }%></p>   edit html: ========= <h1>edit book detail</h1> <%= form_tag(:action=> "update") do%>  <p><label for="book_title">title</label>: <%= text_field 'book', 'title' %></p> <p><label for="book_price">price</label>: <%= text_field 'book', 'price' %></p> <p><label for="book_subject">subject</label>: <%= collection_select(:book, :subject_id,                          @subjects, :id, :name) %></p> <p><label for="book_description">description</label><br/> <%= text_area 'book', 'description' %></p> <%= submit_tag "save changes" %> <%end %> <%= link_to 'back', {:action => 'list' } %> 

i getting following exception when try edit record url http://localhost:3000/book/edit/5,

showing c:/app/app/views/book/edit.html line #5 raised:  undefined method `title' #<array:0x33315c0> extracted source (around line #5):  2: <%= form_tag(:action=> "update") do%> 3:  4: <p><label for="book_title">title</label>: 5: <%= text_field 'book', 'title' %></p> 6: <p><label for="book_price">price</label>: 7: <%= text_field 'book', 'price' %></p> 8: <p><label for="book_subject">subject</label>: 

btw using rails3,ruby1.2 , mysql5.5.

as in learning curve, useful if 1 can me in issue.

for reason you're loading book records when usual intention of edit method edit 1 of them.

to fix this, should define before_filter hook handles loading records:

class bookcontroller < applicationcontroller   # set handler loading book actions, except   # loading single book not relevant.   before_filter :load_book, :except => [ :index, :new, :create ]    def edit     @subjects = subject.find(:all)   end    def update     # call update_attributes method throw exception if     # error occurs.     @book.update_attributes!(params[:book])      redirect_to :action => 'show', :id => @book    rescue activerecord::recordinvalid     # exception triggered if there error saving record     # because of validation problem.      # trigger 'edit' action     edit     # render if on 'edit' page     render :action => 'edit'   end  protected   def load_book     @book = book.find(params[:id])   end end 

as note, time call either find(:all) or all on model, run risk of using system memory , crashing both application , server it's running on. pagination absolutely essential unless can number of records small.

using before_filter makes lot easier consolidate various redundant find calls 1 place , can make error handling lot simpler.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -