图片控制器中的未定义方法

时间:2013-04-09 00:16:48

标签: methods controller routes undefined

警告,我是一个新手,并试图自己学习RoR。正在做教程等,现在试图通过经验学习。长话短说我试图从他们定义的网址附加用户图片,如果他们没有提供默认图片。遇到各种各样的错误,我想我现在已经把它归结为一个主要的问题,经过几个小时的努力来解决这个问题(因为我是新手)。所以现在我得到的错误是:

NoMethodError in GravsController#update
undefined method `update_attributes' for nil:NilClass

我知道这意味着我需要做类似的事情:

@something = Model.new/build(params[:method])

但是我不知道在我的控制器中定义它的位置(我猜对了gravs_controller?)所以这是我的代码,把它撕开,看看你是否可以帮助我让这个图像功能工作,请:)

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 to 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 Your Picture</h1>

    <%= gravs_display @graver %>
        <%= form_for(:grav) do |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(在/ views / gravs /中)

<% provide(:title, "Edit Picture") %>
<div class="barney">
    <div class="trouble">
        <div class="row">
            <div class="span6 offset3">
                <h1>Update Your Picture</h1>

                <%= gravs_display @graver %>

                <%= form_for(:gravs) do |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 do |t|
  t.string :content
  t.integer :user_id
end
end
end

schema.rb

ActiveRecord::Schema.define(:version => 20130407202835) do

create_table "gravs", :force => true do |t|
  t.string  "content"
  t.integer "user_id"
end

create_table "microposts", :force => true do |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 do |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 do
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'

0 个答案:

没有答案