1

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

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(在 /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

架构.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

路线.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'
4

0 回答 0