0

嗨,我目前正在开发我的第一个项目,并尝试在登录/会话之前先构建功能。我正在尝试创建一个相册网站,其中用户有许多相册(包含许多图片),并且相册访问权限在朋友之间共享。但是,我注意到在我之后albums#create

http://localhost:3000/users/18/albums/new(这里没问题)

我被重定向到albums#show

http://localhost:3000/albums/20(问题!!)

URL中也不应该有user_id吗?或者它没有附加到 URL 的 user_id 因为它属于多个用户?这是我的路线:

Pholder::Application.routes.draw do
resources :users do
  resources :albums 
end

resources :albums do
  resources :pictures
end

root :to => "users#index"

这是我的模型以防万一:

用户模型

class User < ActiveRecord::Base

  has_secure_password
  attr_accessible :email, :name, :password, :password_confirmation
  validates_presence_of :password, :on => :create

  validates_format_of :name, :with => /[A-Za-z]+/, :on => :create
  validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, :on => :create
  validates_length_of :password, :minimum => 5, :on => :create

  has_many :user_albums
  has_many :albums, :through => :user_albums
  accepts_nested_attributes_for :albums

end

专辑模特

class Album < ActiveRecord::Base
  attr_accessible :avatar, :name, :description
  has_many :user_albums
  has_many :users, :through => :user_albums
  has_many :photos
end

相片集

class Photo < ActiveRecord::Base
  belongs_to :album
end

专辑控制器

class AlbumsController < ApplicationController

    def index
      @albums = Albums.all

      respond_to do |format|
        format.html
        format.json { render json: @albums }
      end
    end

    def show
      @albums = Album.all
      @album = Album.find(params[:id])
      @photo = Photo.new
    end

    def update
    end

    def edit
    end

    def create
      # @user = User.find(params[:albums][:user_id])
      # @users = User.all
      @album = Album.new(params[:album])
      # @album.user_id = @user.id
      respond_to do |format|
        if @album.save
          format.html { redirect_to @album, notice: 'Album was successfully created.' }
          format.json { render json: @album, status: :created, location: @album}
        else
          format.html { render action: "new" }
          format.json { render json: @album.errors, status: :unprocessable_entity }
        end
      end 
    end

    def new
      @user = User.find(params[:user_id])
      @album = Album.new
    end

    def destroy
    end


end

如果您需要任何其他文件,请告诉我。

4

1 回答 1

0

redirect_to @album行使您重定向到有问题的show操作@album
将这段代码更改为类似redirect_to users_path的内容将使应用程序重定向到index操作users_controller等。
这取决于保存后您想要的任何行为。

阅读本文也应该有所帮助: http: //guides.rubyonrails.org/routing.html

于 2012-10-01T18:40:32.360 回答