-
-
Save RyanRusnak/1153035 to your computer and use it in GitHub Desktop.
class UserpostsController < ApplicationController | |
# GET /userposts | |
# GET /userposts.xml | |
def index | |
@userposts = Userpost.all | |
respond_to do |format| | |
format.html # index.html.erb | |
format.xml { render :xml => @userposts } | |
end | |
end | |
# GET /userposts/1 | |
# GET /userposts/1.xml | |
def show | |
@userpost = Userpost.find(params[:id]) | |
respond_to do |format| | |
format.html # show.html.erb | |
format.xml { render :xml => @userpost } | |
end | |
end | |
# GET /userposts/new | |
# GET /userposts/new.xml | |
def new | |
@userpost = Userpost.new | |
respond_to do |format| | |
format.html # new.html.erb | |
format.xml { render :xml => @userpost } | |
end | |
end | |
# GET /userposts/1/edit | |
def edit | |
@userpost = Userpost.find(params[:id]) | |
end | |
# POST /userposts | |
# POST /userposts.xml | |
def create | |
@user = User.find(params[:user_id]) | |
@userpost = @user.userposts.create(params[:userpost]) | |
redirect_to user_path(@user) | |
end | |
# PUT /userposts/1 | |
# PUT /userposts/1.xml | |
def update | |
@userpost = Userpost.find(params[:id]) | |
respond_to do |format| | |
if @userpost.update_attributes(params[:userpost]) | |
format.html { redirect_to(@userpost, :notice => 'Userpost was successfully updated.') } | |
format.xml { head :ok } | |
else | |
format.html { render :action => "edit" } | |
format.xml { render :xml => @userpost.errors, :status => :unprocessable_entity } | |
end | |
end | |
end | |
# DELETE /userposts/1 | |
# DELETE /userposts/1.xml | |
def destroy | |
@userpost = Userpost.find(params[:id]) | |
@userpost.destroy | |
respond_to do |format| | |
format.html { redirect_to(userposts_url) } | |
format.xml { head :ok } | |
end | |
end | |
end |
LoginApp::Application.routes.draw do | |
resources :gymposts | |
resources :userposts | |
resources :gyms, :order => 'totalBench DESC' | |
resources :users, :user_sessions | |
match 'login' => 'user_sessions#new', :as => :login | |
match 'logout' => 'user_sessions#destroy', :as => :logout | |
resources :gyms do | |
resources :gymposts, :users do | |
resources :userposts do | |
end | |
end | |
end | |
get '/leaderboard', :to => 'users#leaderboard' | |
end |
<p id="notice"><%= notice %></p> | |
<p> | |
<b>Username:</b> | |
<%= @user.username %> | |
</p> | |
<p> | |
<b>Email:</b> | |
<%= @user.email %> | |
</p> | |
<!-- | |
<p> | |
<b>Crypted password:</b> | |
<%= @user.crypted_password %> | |
</p> | |
<p> | |
<b>Password salt:</b> | |
<%= @user.password_salt %> | |
</p> | |
<p> | |
<b>Persistence token:</b> | |
<%= @user.persistence_token %> | |
</p> | |
--> | |
<p> | |
<b>Age:</b> | |
<%= @user.age %> | |
</p> | |
<p> | |
<b>Weight:</b> | |
<%= @user.weight %> | |
</p> | |
<p> | |
<b>Max bench:</b> | |
<%= @user.maxBench %> | |
</p> | |
<p> | |
<b>Max Squat:</b> | |
<%= @user.maxSquat %> | |
</p> | |
<p> | |
<b>Gym:</b> | |
<%= @user.gym.name %> | |
</p> | |
<p> | |
<%= image_tag @user.photo.url(:small) %> | |
</p> | |
<%= link_to 'Edit', edit_user_path(@user) %> | | |
<%= link_to 'Back', users_path %> | |
<h1>Listing userposts</h1> | |
<table> | |
<tr> | |
<th>Body</th> | |
<th></th> | |
<th></th> | |
<th></th> | |
</tr> | |
<% @user.userposts.each do |userpost| %> | |
<tr> | |
<td><%= userpost.body %></td> | |
<td><%= link_to 'Show', userpost %></td> | |
<td><%= link_to 'Edit', edit_userpost_path(userpost) %></td> | |
<td><%= link_to 'Destroy', userpost, :confirm => 'Are you sure?', :method => :delete %></td> | |
</tr> | |
<% end %> | |
</table> | |
<br /> | |
<h2>Add a comment:</h2> | |
<%= form_for [@current_user, Userpost.new] do |f| %> | |
<div class="field"> | |
<%= f.label :body %><br /> | |
<%= f.text_area :body %> | |
<%= f.hidden_field :user_id %> | |
</div> | |
<div class="actions"> | |
<%= f.submit %> | |
</div> | |
<% end %> |
activerecord (3.0.5) lib/active_record/relation/finder_methods.rb:279:in `find_with_ids' | |
activerecord (3.0.5) lib/active_record/relation/finder_methods.rb:107:in `find' | |
activerecord (3.0.5) lib/active_record/base.rb:439:in `__send__' | |
activerecord (3.0.5) lib/active_record/base.rb:439:in `find' | |
app/controllers/userposts_controller.rb:43:in `create' | |
actionpack (3.0.5) lib/action_controller/metal/implicit_render.rb:4:in `send_action' | |
actionpack (3.0.5) lib/action_controller/metal/implicit_render.rb:4:in `send_action' | |
actionpack (3.0.5) lib/abstract_controller/base.rb:150:in `process_action' | |
actionpack (3.0.5) lib/action_controller/metal/rendering.rb:11:in `process_action' | |
actionpack (3.0.5) lib/abstract_controller/callbacks.rb:18:in `process_action' | |
activesupport (3.0.5) lib/active_support/callbacks.rb:440:in `_run__961832392__process_action__1623385099__callbacks' | |
activesupport (3.0.5) lib/active_support/callbacks.rb:409:in `send' | |
activesupport (3.0.5) lib/active_support/callbacks.rb:409:in `_run_process_action_callbacks' | |
activesupport (3.0.5) lib/active_support/callbacks.rb:93:in `send' | |
activesupport (3.0.5) lib/active_support/callbacks.rb:93:in `run_callbacks' | |
actionpack (3.0.5) lib/abstract_controller/callbacks.rb:17:in `process_action' | |
actionpack (3.0.5) lib/action_controller/metal/instrumentation.rb:30:in `process_action' | |
activesupport (3.0.5) lib/active_support/notifications.rb:52:in `instrument' | |
activesupport (3.0.5) lib/active_support/notifications/instrumenter.rb:21:in `instrument' | |
activesupport (3.0.5) lib/active_support/notifications.rb:52:in `instrument' | |
actionpack (3.0.5) lib/action_controller/metal/instrumentation.rb:29:in `process_action' | |
actionpack (3.0.5) lib/action_controller/metal/rescue.rb:17:in `process_action' | |
actionpack (3.0.5) lib/abstract_controller/base.rb:119:in `process' | |
actionpack (3.0.5) lib/abstract_controller/rendering.rb:41:in `process' | |
actionpack (3.0.5) lib/action_controller/metal.rb:138:in `dispatch' | |
actionpack (3.0.5) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch' | |
actionpack (3.0.5) lib/action_controller/metal.rb:178:in `action' | |
actionpack (3.0.5) lib/action_dispatch/routing/route_set.rb:62:in `call' | |
actionpack (3.0.5) lib/action_dispatch/routing/route_set.rb:62:in `dispatch' | |
actionpack (3.0.5) lib/action_dispatch/routing/route_set.rb:27:in `call' | |
rack-mount (0.6.13) lib/rack/mount/route_set.rb:148:in `call' | |
rack-mount (0.6.13) lib/rack/mount/code_generation.rb:93:in `recognize' | |
rack-mount (0.6.13) lib/rack/mount/code_generation.rb:68:in `optimized_each' | |
rack-mount (0.6.13) lib/rack/mount/code_generation.rb:92:in `recognize' | |
rack-mount (0.6.13) lib/rack/mount/route_set.rb:139:in `call' | |
actionpack (3.0.5) lib/action_dispatch/routing/route_set.rb:492:in `call' | |
actionpack (3.0.5) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call' | |
actionpack (3.0.5) lib/action_dispatch/middleware/head.rb:14:in `call' | |
rack (1.2.1) lib/rack/methodoverride.rb:24:in `call' | |
actionpack (3.0.5) lib/action_dispatch/middleware/params_parser.rb:21:in `call' | |
actionpack (3.0.5) lib/action_dispatch/middleware/flash.rb:182:in `call' | |
actionpack (3.0.5) lib/action_dispatch/middleware/session/abstract_store.rb:149:in `call' | |
actionpack (3.0.5) lib/action_dispatch/middleware/cookies.rb:302:in `call' | |
activerecord (3.0.5) lib/active_record/query_cache.rb:32:in `call' | |
activerecord (3.0.5) lib/active_record/connection_adapters/abstract/query_cache.rb:28:in `cache' | |
activerecord (3.0.5) lib/active_record/query_cache.rb:12:in `cache' | |
activerecord (3.0.5) lib/active_record/query_cache.rb:31:in `call' | |
activerecord (3.0.5) lib/active_record/connection_adapters/abstract/connection_pool.rb:354:in `call' | |
actionpack (3.0.5) lib/action_dispatch/middleware/callbacks.rb:46:in `call' | |
activesupport (3.0.5) lib/active_support/callbacks.rb:415:in `_run_call_callbacks' | |
actionpack (3.0.5) lib/action_dispatch/middleware/callbacks.rb:44:in `call' | |
rack (1.2.1) lib/rack/sendfile.rb:107:in `call' | |
actionpack (3.0.5) lib/action_dispatch/middleware/remote_ip.rb:48:in `call' | |
actionpack (3.0.5) lib/action_dispatch/middleware/show_exceptions.rb:47:in `call' | |
railties (3.0.5) lib/rails/rack/logger.rb:13:in `call' | |
rack (1.2.1) lib/rack/runtime.rb:17:in `call' | |
activesupport (3.0.5) lib/active_support/cache/strategy/local_cache.rb:72:in `call' | |
rack (1.2.1) lib/rack/lock.rb:11:in `call' | |
rack (1.2.1) lib/rack/lock.rb:11:in `synchronize' | |
rack (1.2.1) lib/rack/lock.rb:11:in `call' | |
actionpack (3.0.5) lib/action_dispatch/middleware/static.rb:30:in `call' | |
railties (3.0.5) lib/rails/application.rb:168:in `call' | |
railties (3.0.5) lib/rails/application.rb:77:in `send' | |
railties (3.0.5) lib/rails/application.rb:77:in `method_missing' | |
railties (3.0.5) lib/rails/rack/log_tailer.rb:14:in `call' | |
rack (1.2.1) lib/rack/content_length.rb:13:in `call' | |
rack (1.2.1) lib/rack/handler/webrick.rb:52:in `service' | |
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/httpserver.rb:104:in `service' | |
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/httpserver.rb:65:in `run' | |
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/server.rb:173:in `start_thread' | |
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/server.rb:162:in `start' | |
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/server.rb:162:in `start_thread' | |
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/server.rb:95:in `start' | |
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/server.rb:92:in `each' | |
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/server.rb:92:in `start' | |
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/server.rb:23:in `start' | |
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/server.rb:82:in `start' | |
rack (1.2.1) lib/rack/handler/webrick.rb:13:in `run' | |
rack (1.2.1) lib/rack/server.rb:213:in `start' | |
railties (3.0.5) lib/rails/commands/server.rb:65:in `start' | |
railties (3.0.5) lib/rails/commands.rb:30 | |
railties (3.0.5) lib/rails/commands.rb:27:in `tap' | |
railties (3.0.5) lib/rails/commands.rb:27 | |
script/rails:6:in `require' | |
script/rails:6 |
change_column :userposts, :post_id, :user_id
user = User.create
user.class
user.posts.class
- Fabricator
- Forgery
- db/seed.rb
This is the reason it wasn't working: <%= f.hidden_field :user_id %>
You're setting a hidden field user_id, which is nil, because you're not setting it to anything.
Then when you @user.userposts.create(params[:userposts])
, the params[:userposts][:user_id]
(nil) overrides the user_id that would usually get set by association. Take out that hidden field, and you should be able to create it like you originally did: @user.userposts.create(params[:userpost])
Cheers,
Edward
Also, did you understand why the routes needed to change?
aaaaah i see. I think the routes were the main problem. Adding the hidden field was a desperate attempt to fix the error. I probably tried a hundred other things before that. I should have went right for the routes!
rails generate migration Post body:text user_id:integer --timestamps