我在我的应用中设置了多态关联。图片属于记分板模型和用户模型两者都是可图像的。每个记分牌和用户都有一张图片。我还有嵌套路由,其中图片资源嵌套在用户和记分板资源中。
用户的路由文件:
resources :users do
resources :picture
end
resources :account_activations, only: [:edit]
resources :password_resets, only: [:new, :create, :edit, :update]
resources :conversations, only: [:index, :show, :destroy] do
member do
post :reply
post :restore
post :mark_as_read
end
collection do
delete :empty_trash
end
end
记分板的路由文件:
resources :scoreboards do
member do
put :favourite
get :deleteteams
get :deleteschedules
end
resources :invitations, only: [:new, :create]
resources :comments
resources :teams, only: [:edit, :create, :destroy, :update]
resources :schedules
resources :picture
end
我已经设置了一个图片控制器。代码如下。
class PictureController < ApplicationController
before_filter :load_pictureable
def new
@picture = @pictureable.build_picture
end
def create
end
def destroy
end
private
def picture_params
params.require(:picture).permit(:picture)
end
def load_pictureable
klass = [User, Scoreboard].detect { |c| params["#{c.name.underscore}_id"] }
@pictureable = klass.find(params["#{klass.name.underscore}_id"])
end
end
我的新表格如下:
<div id= "uploadphoto">
<%= form_for([@pictureable, @picture], url: scoreboard_picture_path, html: {multipart: true}) do |f| %>
<%= f.file_field :picture, accept: 'image/jpeg,image/gif,image/png', id: "files", class: "hidden" %>
<label for="files" id="picture"><h4 id="picturetext">Click Here to Upload Picture</h4></label>
<div class="modal" id="modal-1">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">Position and Crop your Image</div>
<div class="modal-body">
<div class="upload-preview">
<img id="image" /></img>
</div>
<%= f.submit "upload photo" %>
</div>
<div class="modal-footer"> Click Anywhere Outside the box to Cancel</div>
</div>
</div>
</div>
<% end %>
</div>
我遇到的问题是网址。这给了我以下错误。
No route matches {:action=>"show", :controller=>"picture", :scoreboard_id=>"13"} missing required keys: [:id]
我不确定为什么它会将我重定向到show动作,我认为它可能是嵌套路线。我不知道问题是什么。我以为我通过传递以下网址new_scoreboard_picture_path
解决了这个问题。但是,当我按上传照片时,它会将我带回相同的网址。这是有道理的,因为我明确说明了新表单中的URL。但是,这是不正确的。如果有人可以帮助我弄清问题是什么,那将是一个很大的帮助。我只包含了相关的代码。但是,如果我遗失了什么,请告诉我。关于我为什么会收到此错误的任何解释也会澄清我的过程。这是第一次使用多态关联。任何形式的帮助都会很棒!!
答案 0 :(得分:0)
我会使用继承和两个独立的控制器:
# the base controller
class PicturesController < ApplicationController
before_action :load_pictureable
def new
@picture = @pictureable.build_picture
end
def create
@picture = @pictureable.build_picture
if @picture.save
do_redirect
else
render_errors
end
end
def destroy
# ...
end
private
def picture_params
params.require(:picture).permit(:picture)
end
# Finds the picturable based on the module name of the class
# for examples `Pets::PicturesController` will look for
# Pet.find(params[:pet_id])
def find_pictureable
# this looks at the class name and extracts the module name
model_name = self.class.deconstantize.singularize
@pictureable = model_name.constanize.find(params["#{model_name}_id"])
end
# subclasses can override this to whatever they want
def do_redirect
redirect_to @pictureable
end
# subclasses can override this to whatever they want
def render_errors
render :new
end
end
然后设置路线:
resources :users do
resource :picture, controller: 'users/pictures'
end
resources :scoreboards do
resource :picture, controller: 'scoreboards/pictures'
end
由于用户/记分板只能包含一张我们使用resource
而不是resources
的图片。这为奇异资源设置了路径。使用rake routes
查看自己的差异。例如,您使用POST /users/:user_id/picture
创建了一张图片。
然后我们设置子控制器:
# app/controllers/users/pictures_controller.rb
class Users::PicturesController < PicturesController
end
# app/controllers/scoreboards/pictures_controller.rb
class Scoreboards::PicturesController < PicturesController
end
这样可以实现高度自定义,而不会出现混杂的if/else
或switch
语句。此外,这可以防止恶意用户通过params[:user_id]
来操纵用户图片。您永远不应该使用用户输入来确定类或数据库表。
此外,您无需明确指定表单url:
<%= form_for([@pictureable, @picture], html: {multipart: true}) do |f| %>
将使用users_picture_path(user_id: @pictureable.id)
或相同的记分板。