ruby on rails - how do I correctly make an active record query from one table to another? -
been stuck on afternoon/evening. appreciate continue work on this.
i have 2 different tables. posts (status , photo) , places (address). i'd combine these (status, photo, address) 1 form , 1 show.
so, think having trouble making active record query interface. however, may have messed in associating tables...
post.rb model
class post < activerecord::base belongs_to :place belongs_to :user has_many :comments has_many :commenters, through: :comments, source: :user end place.rb model
class place < activerecord::base has_many :posts end posts _form.html.erb
<%= form_for(@post) |f| %> <% if @post.errors.any? %> <div id="error_explanation"> <h2><%= pluralize(@post.errors.count, "error") %> prohibited post being saved:</h2> <ul> <% @post.errors.full_messages.each |msg| %> <li><%= msg %></li> <% end %> </ul> </div> <% end %> <div class="field"> <%= f.label :status %><br> <%= f.text_field :status %> </div> <div class="field"> <%= f.label :upload %><br> <%= f.text_field :upload %> </div> <%= f.label :place %><br> <%= f.text_field :place %> <div class="actions"> <%= f.submit %> </div> <% end %> posts show.html.erb
<h1><%= @post.status %></h1> <p><%= link_to @post.upload, @post.upload %></p> <p><%= @post.place %></p> <%= form_for [@post, @comment] |f| %> <p> <%= f.text_area :response, placeholder: "add comment...", :rows => 8, :cols => 40 %> </p> <%= f.submit %> <% end %> <ul class="comments"> <% @post.comments.each |comment| %> <li><%= "#{comment.user.try(:email)} posted: #{comment.response}" %></li> <% end %> </ul> <p><%= link_to "back home page", root_path %></p> posts_controller.rb
class postscontroller < applicationcontroller before_action :set_post, only: [:show, :edit, :update, :destroy] # /posts # /posts.json def index @posts = post.all end # /posts/1 # /posts/1.json def show @post = post.find params[:id] @comment = comment.new(:post => @post) end # /posts/new def new @post = post.new end # /posts/1/edit def edit end def create safe_post = params.require(:post).permit(:status, :upload) @post = current_user.posts.new safe_post @post.place = place.from_params params[:place] @post.save redirect_to @post end # patch/put /posts/1 # patch/put /posts/1.json def update respond_to |format| if @post.update(post_params) format.html { redirect_to @post, notice: 'post updated.' } format.json { head :no_content } else format.html { render action: 'edit' } format.json { render json: @post.errors, status: :unprocessable_entity } end end end # delete /posts/1 # delete /posts/1.json def destroy @post.destroy respond_to |format| format.html { redirect_to posts_url } format.json { head :no_content } end end private # use callbacks share common setup or constraints between actions. def set_post @post = post.find(params[:id]) end # never trust parameters scary internet, allow white list through. def post_params params.require(:post).permit(:status, :upload) end end places_controller.rb
class placescontroller < applicationcontroller before_action :set_place, only: [:show, :edit, :update, :destroy] # /places # /places.json def index @places = place.all end # /places/1 # /places/1.json def show @place = place.find params[:id] end # /places/new def new @place = place.new end # /places/1/edit def edit end # post /places # post /places.json def create @place = place.new(place_params) respond_to |format| if @place.save format.html { redirect_to @place, notice: 'place created.' } format.json { render action: 'show', status: :created, location: @place } else format.html { render action: 'new' } format.json { render json: @place.errors, status: :unprocessable_entity } end end end # patch/put /places/1 # patch/put /places/1.json def update respond_to |format| if @place.update(place_params) format.html { redirect_to @place, notice: 'place updated.' } format.json { head :no_content } else format.html { render action: 'edit' } format.json { render json: @place.errors, status: :unprocessable_entity } end end end # delete /places/1 # delete /places/1.json def destroy @place.destroy respond_to |format| format.html { redirect_to places_url } format.json { head :no_content } end end private # use callbacks share common setup or constraints between actions. def set_place @place = place.find(params[:id]) end # never trust parameters scary internet, allow white list through. def place_params params.require(:place).permit(:address, :latitude, :longitude) end end schema.rb
activerecord::schema.define(version: 20140324213459) create_table "comments", force: true |t| t.integer "user_id" t.integer "post_id" t.string "response" t.datetime "created_at" t.datetime "updated_at" end add_index "comments", ["post_id"], name: "index_comments_on_post_id" add_index "comments", ["user_id"], name: "index_comments_on_user_id" create_table "places", force: true |t| t.string "address" t.float "latitude" t.float "longitude" t.datetime "created_at" t.datetime "updated_at" end create_table "posts", force: true |t| t.string "status" t.string "upload" t.datetime "created_at" t.datetime "updated_at" t.integer "user_id" t.integer "place_id" end add_index "posts", ["place_id"], name: "index_posts_on_place_id" add_index "posts", ["user_id"], name: "index_posts_on_user_id" create_table "users", force: true |t| t.string "name" t.string "photo" t.string "bio" t.datetime "created_at" t.datetime "updated_at" t.string "email", default: "", null: false t.string "encrypted_password", default: "", null: false t.string "reset_password_token" t.datetime "reset_password_sent_at" t.datetime "remember_created_at" t.integer "sign_in_count", default: 0, null: false t.datetime "current_sign_in_at" t.datetime "last_sign_in_at" t.string "current_sign_in_ip" t.string "last_sign_in_ip" end add_index "users", ["email"], name: "index_users_on_email", unique: true add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true end routes.rb
dondeapp::application.routes.draw resources :posts resources :comments, only: :create resources :places end devise_for :users root 'posts#index' end
you need nested model form, http://railscasts.com/episodes/196-nested-model-form-part-1
Comments
Post a Comment