Ruby on rails Rails 3:理解此请求的MVC时遇到问题
如果你熟悉railscasts,我遵循#258 token field最初建立了它,ryan bates books通过作者身份拥有许多作者,我使用posts通过手工艺品拥有许多艺术家。(和帖子属于用户) 换句话说: 我有一个用户用字段title和artist创建的帖子,帖子表单包含一个名为artist_tokens的artist字段的虚拟属性,这个虚拟属性有一个名为artist_tokens=(id)的关联setter方法 这就是让我理解问题的方法。这个方法有两件事。它接受post表单中输入的ID并调用Artist.create!使用ID作为:名称。然后它在剩下的任何Artister对象上调用.id。这是用于创建Artisship association的最终艺术家id 1) 当我从post模型内部调用Artist.create(:name=>$1)时发生了什么 2) 然后该请求是否转到Artister控制器内的create方法 我知道一个普通的post请求会转到postscontroller#create方法,然后写入数据库,然后重定向到create方法中的任何路径,例如post#path,因此它会点击postscontroller#show,然后进入视图。对吗 3) 当我调用Artist.create时,从控制器到模型的请求顺序是什么!从post模型中的artist_tokens=(ids)方法Ruby on rails Rails 3:理解此请求的MVC时遇到问题,ruby-on-rails,ruby-on-rails-3,model-view-controller,associations,Ruby On Rails,Ruby On Rails 3,Model View Controller,Associations,如果你熟悉railscasts,我遵循#258 token field最初建立了它,ryan bates books通过作者身份拥有许多作者,我使用posts通过手工艺品拥有许多艺术家。(和帖子属于用户) 换句话说: 我有一个用户用字段title和artist创建的帖子,帖子表单包含一个名为artist_tokens的artist字段的虚拟属性,这个虚拟属性有一个名为artist_tokens=(id)的关联setter方法 这就是让我理解问题的方法。这个方法有两件事。它接受post表单中输入的
# app/models/user.rb
class User < ActiveRecord::Base
has_many :posts, :dependent => :destroy
has_many :artists, :through => :posts
attr_accessible :email, :password, :password_confirmation,
:remember_me, :name, :avatar, :username, :bio
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
mount_uploader :avatar, AvatarUploader
end
# app/models/post.rb
class Post < ActiveRecord::Base
belongs_to :user
has_many :artisanships
has_many :artists, :through => :artisanships
attr_accessible :title, artist_tokens
attr_reader :artist_tokens
def artist_tokens=(ids)
ids.gsub!(/CREATE_(.+?)_END/) do
Artist.create!(:name => $1).id
end
self.artist_ids = ids.split(",")
end
end
# all/models/artist.rb
class Artist < ActiveRecord::Base
has_many :artisanships
has_many :users, :through => :posts
has_many :posts, :through => :artisanships
attr_accessible :name
end
# app/models/artisanship.rb
class Artisanships < ActiveRecord::Base
belongs_to :post
belongs_to :artist
has_one :user, :through => :post
attr_accessible :post_id, :artist_id
end
# app/views/shared/_post_form.html.erb
<%= form_for @post, :validate => true, :html => {:multipart => true} do |f| %>
<%= render 'shared/error_messages', :object => f.object %>
<div class="field">
<%= f.label :title, 'Title:' %><br />
<%= f.text_field :title %><br />
<%= f.label :artist_tokens, "Artists" %><br />
<%= f.text_field :artist_tokens, "data-pre" =>
@post.artists.map(&:attributes).to_json %> # html 5 data attribute for prepopulate
</div>
<div class="actions">
<%= f.submit "Submit" %>
</div>
<% end %>
# app/controllers/pages_controller.rb
class PagesController < ApplicationController
def home
@title = "Home"
@featured_posts = Post.featured.limit(10)
if user_signed_in?
@user = current_user
@post = current_user.posts.build
@feed_items = current_user.feed.paginate(:per_page => "10", :page => params[:page])
else
#render :layout => 'special_layout'
end
end
# app/controllers/posts_controller.rb
class PostsController < ApplicationController
before_filter :authenticate_user!, :only => [:create, :edit, :update, :destroy]
before_filter :authorized_user, :only => [:destroy, :edit, :update]
def create
@user = current_user
@post = current_user.posts.build(params[:post])
if @post.save
flash[:success] = "Post created!"
redirect_to root_path
else
@feed_items = current_user.feed.paginate(:per_page => "10", :page => params[:page])
render 'pages/home'
end
end
def index
@posts = Post.paginate(:page => params[:page])
end
def show
@post = Post.find(params[:id])
end
def edit
@post = Post.find(params[:id])
end
def update
@post = Post.find(params[:id])
respond_to do |format|
if @post.update_attributes(params[:post])
format.html { redirect_to(post_path(@post), :notice => 'Post was successfully updated.') }
else
format.html { render :action => "edit" }
end
end
end
def destroy
@post.destroy
redirect_to root_path
end
def likers
@title = "Likers"
@post = Post.find(params[:id])
@likers = @post.likers.paginate(:page => params[:page])
render 'show_likers'
end
def search
if params[:q]
query = params[:q]
@search = Post.search do
keywords query
end
@posts = @search.results
end
end
private
def authorized_user
@post = Post.find(params[:id])
redirect_to root_path unless current_user?(@post.user)
end
end
# app/controller/artists_controller.rb
class ArtistsController < ApplicationController
def index
@artists = Artist.where("name like ?", "%#{params[:q]}%")
results = @artists.map(&:attributes)
results << {:name => "Add: #{params[:q]}", :id => "CREATE_#{params[:q]}_END"}
respond_to do |format|
format.html
format.json { render :json => results }
end
end
def show
@artist = Artist.find(params[:id])
end
def new
@artist = Artist.new
end
def create
@artist = Artist.build(params[:artist])
if @artist.save
redirect_to @artist, :notice => "Successfully created artist."
else
render :action => 'new'
end
end
def edit
@artist = Artist.find(params[:id])
end
def update
@artist = Artist.find(params[:id])
if @artist.update_attributes(params[:artist])
redirect_to @artist, :notice => "Successfully updated artist."
else
render :action => 'edit'
end
end
def destroy
@artist = Artist.find(params[:id])
@artist.destroy
redirect_to authors_url, :notice => "Successfully destroyed artist."
end
end
#app/models/user.rb
类用户:destroy
有很多:艺术家,:通过=>:帖子
属性可访问:电子邮件、:密码、:密码确认、,
:记住我,:姓名,:头像,:用户名,:个人简历
设计:数据库可验证,可注册,
:可恢复,:可记忆,:可跟踪,:可验证
装载上传者:阿凡达,阿凡达上传者
结束
#app/models/post.rb
类Post:手工艺品
可访问属性:标题、艺术家标记
属性阅读器:艺术家标记
def艺术家_令牌=(ID)
ids.gsub!(/CREATE.+?)\u END/)do
艺术家,创造!(:name=>1美元).id
结束
self.artist_id=id.split(“,”)
结束
结束
#所有/models/artist.rb
类艺术家:帖子
有很多:帖子,:到=>:手工艺品
可访问属性:名称
结束
#app/models/Artisship.rb
类Artiships:post
属性可访问:post\u id,:artist\u id
结束
#app/views/shared/_post_form.html.erb
true,:html=>{:multipart=>true}do | f |%>
f、 对象%>
@post.artists.map(&:attributes).to_json%>#用于预填充的HTML5数据属性
#app/controllers/pages\u controller.rb
类PagesController<应用程序控制器
def主页
@title=“主页”
@特色文章=文章特色文章限制(10)
如果用户已登录?
@用户=当前用户
@post=当前_user.posts.build
@feed\u items=current\u user.feed.paginate(:per\u page=>10,:page=>params[:page])
其他的
#render:layout=>“特殊布局”
结束
结束
#app/controllers/posts_controller.rb
类PostsController<应用程序控制器
在\u筛选器之前:验证\u用户!,:仅=>[:创建、编辑、更新、销毁]
_filter:authorized_user之前,:only=>[:destroy,:edit,:update]
def创建
@用户=当前用户
@post=当前用户.posts.build(参数[:post])
如果@post.save
flash[:success]=“创建帖子!”
将\重定向到根\路径
其他的
@feed\u items=current\u user.feed.paginate(:per\u page=>10,:page=>params[:page])
呈现“页面/主页”
结束
结束
def索引
@posts=Post.paginate(:page=>params[:page])
结束
def秀
@post=post.find(参数[:id])
结束
定义编辑
@post=post.find(参数[:id])
结束
def更新
@post=post.find(参数[:id])
回应待办事项|格式|
如果@post.update_属性(参数[:post])
format.html{redirect_to(post_路径(@post),:notice=>“post已成功更新”。)}
其他的
format.html{render:action=>“edit”}
结束
结束
结束
def销毁
@事后销毁
将\重定向到根\路径
结束
DEFLIKERS
@title=“Likers”
@post=post.find(参数[:id])
@likers=@post.likers.paginate(:page=>params[:page])
呈现“展示喜爱者”
结束
def搜索
如果参数[:q]
query=params[:q]
@search=Post.search do
关键词查询
结束
@posts=@search.results
结束
结束
私有的
def授权用户
@post=post.find(参数[:id])
将\重定向到根\路径,除非当前\用户?(@post.user)
结束
结束
#app/controller/artists\u controller.rb
类ArtistsController<应用程序控制器
def索引
@美术师=美术师。其中(“名字像?”,“%#{params[:q]}%”)
结果=@artists.map(&:attributes)
结果“添加:{params[:q]}”,:id=>“创建”{params[:q]}}
回应待办事项|格式|
format.html
format.json{render:json=>results}
结束
结束
def秀
@艺术家=艺术家.find(参数[:id])
结束
def新
@艺术家=艺术家
结束
def创建
@艺术家=艺术家.构建(参数[:艺术家])
如果@artist.save
将_重定向到@artist,:notice=>“已成功创建艺术家。”
其他的
呈现:操作=>“新建”
结束
结束
定义编辑
@艺术家=艺术家.find(参数[:id])
结束
def更新
@艺术家=艺术家.find(参数[:id])
如果@artist.update_属性(参数[:artist])
将_重定向到@artist,:notice=>“已成功更新artist。”
其他的
呈现:操作=>“编辑”
结束
结束
def销毁
@艺术家=艺术家.find(参数[:id])
@毁灭
重定向_