Undefined Method in Picture Controller -
warning, i'm newb , trying learn ror on own. been doing tutorials etc , trying learn through experience. long story short i'm trying attach user picture url defined them, default pic if don't supply one. been running kinds of errors, think have down 1 primary issue after hours of trying figure out (since i'm new @ this). right error i'm getting is:
nomethoderror in gravscontroller#update undefined method `update_attributes' nil:nilclass
i know means need like:
@something = model.new/build(params[:method])
but don't know define in controller (i'm guessing gravs_controller right?) here's code, tear apart , see if can me image function working, please :)
class gravscontroller < applicationcontroller before_filter :signed_in_user
gravs_controller
def create @graver = current_user.gravs.new(params[:content]) @grav_bool = false if @graver.save @grav_bool = true @grav_bool.save flash[:success] = "user picture saved!" redirect_to faq_path else @grav_bool = false @grav_bool.save redirect_to faq_path end end def update if @graver.update_attributes(params[:content]) @grav_bool = true @grav_bool.save flash[:success] = "user picture saved!" redirect_to faq_path else @grav_bool = false @grav_bool.save redirect_to faq_path end end end
users_controller
class userscontroller < applicationcontroller before_filter :signed_in_user, only: [:index, :edit, :update, :destroy, :gravs] before_filter :correct_user, only: [:edit, :update, :gravs] before_filter :admin_user, only: :destroy def index @users = user.paginate(page: params[:page]) end def show @user = user.find(params[:id]) @microposts = @user.microposts.paginate(page: params[:page]) end def new @user = user.new end def create @user = user.new(params[:user]) if @user.save sign_in @user flash[:success] = "welcome inception!" redirect_to gravs_path else render 'new' end end def edit end def update if @user.update_attributes(params[:user]) sign_in @user flash[:success] = "profile updated" redirect_to gravsid_path else render 'edit' end end def destroy user.find(params[:id]).destroy flash[:success] = "user deleted" redirect_to users_path end private def correct_user @user = user.find(params[:id]) redirect_to (root_path), error: "cannot edit others information!" unless current_user?(@user) end def admin_user redirect_to(root_path) unless current_user.admin? end end
create.html.erb (inside /views/gravs/)
<% provide(:title, "edit picture") %> <div class="barney"> <div class="trouble"> <h1>update picture</h1> <%= gravs_display @graver %> <%= form_for(:grav) |f| %> <div class="field"> <%= f.text_field :content, placeholder: "enter picture url" %> </div> <%= f.submit "update user image", class: "btn btn-small btn-primary" %> <% end %> </div> </div>
update.html.erb (inside /views/gravs/)
<% provide(:title, "edit picture") %> <div class="barney"> <div class="trouble"> <div class="row"> <div class="span6 offset3"> <h1>update picture</h1> <%= gravs_display @graver %> <%= form_for(:gravs) |f| %> <div class="field"> <%= f.text_field :content, placeholder: "update picture url" %> </div> <%= f.submit "update user image", class: "btn btn-small btn-primary" %> <% end %> </div> </div> </div> </div>
** datenumber_create_gravs.rb**
class creategravs < activerecord::migration def change create_table :gravs |t| t.string :content t.integer :user_id end end end
schema.rb
activerecord::schema.define(:version => 20130407202835) create_table "gravs", :force => true |t| t.string "content" t.integer "user_id" end create_table "microposts", :force => true |t| t.string "content" t.integer "user_id" t.datetime "created_at", :null => false t.datetime "updated_at", :null => false end add_index "microposts", ["user_id", "created_at"], :name => "index_microposts_on_user_id_and_created_at" create_table "users", :force => true |t| t.string "name" t.string "email" t.datetime "created_at", :null => false t.datetime "updated_at", :null => false t.string "password_digest" t.string "remember_token" t.boolean "admin", :default => false end add_index "users", ["email"], :name => "index_users_on_email", :unique => true add_index "users", ["remember_token"], :name => "index_users_on_remember_token" end
routes.rb
mydeasample::application.routes.draw resources :users resources :sessions, only: [:new, :create, :destroy] resources :microposts, only: [:create, :destroy] root to: "static_pages#home" match '/help', to: 'static_pages#help' match '/about', to: 'static_pages#about' match '/contact', to: 'static_pages#contact' match '/signup', to: 'users#new' match '/signin', to: 'sessions#new' match '/signout', to: 'sessions#destroy', via: :delete match '/message', to: 'static_pages#message' match '/faq', to: 'static_pages#faq' match '/gravs', to: 'gravs#create' match '/gravsid', to: 'gravs#update'
Comments
Post a Comment