I have a comments model that belongs to two models: submissions and posts
class Comment < ActiveRecord::Base
attr_accessible :content, :show
belongs_to :commentable, :polymorphic => true
end
class Submission < ActiveRecord::Base
has_many :comments, :as => :commentable, :dependent => :destroy
end
Submissions is a nested route and post is not.
In my comments controller:
def create
#commentable = find_commentable
#comment = #commentable.comments.build(params[:comment])
#comment.user = current_user
if #comment.save
#CommentMailer.comment_email(#user, #comment, #commentable).deliver
flash[:notice] = "Successfully created comment."
if #commentable == #submission
redirect_to [#contest, #commentable]
else
redirect_to [#commentable]
end
else
render :action => 'new'
end
end
find_contest
def find_contest
#contest = Contest.find(params[:contest_id])
end
find_commentable:
def find_commentable
params.each do |name, value|
if name =~ /(.+)_id$/
return $1.classify.constantize.find(value)
end
end
nil
end
The redirect to post via #commentable works fine, but the redirect to submissions is not finding the contest.
Started POST "/submissions/36/comments" for 127.0.0.1 at 2012-11-30 18:34:41 -0800
Processing by CommentsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"R62NH5/EE34FPapEqy7mfpa0wKz18GtSdhH8MGYq2Ec=", "comment"=>{"content"=>"test", "show"=>"true"}, "commit"=>"Create Comment", "submission_id"=>"36"}
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = 2 ORDER BY users.created_at DESC LIMIT 1
Submission Load (0.3ms) SELECT "submissions".* FROM "submissions" WHERE "submissions"."id" = $1 ORDER BY submissions.created_at DESC LIMIT 1 [["id", "36"]]
Completed 500 Internal Server Error in 116ms
ActiveRecord::RecordNotFound (Couldn't find Contest without an ID):
app/controllers/comments_controller.rb:19:in `create'
Change to submission routes:
submissions GET /submissions(.:format) submissions#index
POST /submissions(.:format) submissions#create
new_submission GET /submissions/new(.:format) submissions#new
edit_submission GET /submissions/:id/edit(.:format) submissions#edit
submission GET /submissions/:id(.:format) submissions#show
PUT /submissions/:id(.:format) submissions#update
DELETE /submissions/:id(.:format) submissions#destroy
Submission form:
<%= simple_form_for #submission, :html => { :multipart => true } do |f| %>
<div class="span7 offset2 submission">
<fieldset class="well pleft80 edit">
<%= f.hidden_field :contest_id , :value => params[:contest_id] %>
<%= f.input :title %>
<%= f.input :description %>
<%= f.input :comment_show, :as => :hidden, :input_html => { :value => true } %>
</fieldset>
<fieldset class="well pleft80 noborder">
<%= f.fields_for :image do |img_field| %>
<h3>Upload Photo<%= img_field.file_field :source %></h3>
<% end %>
</fieldset>
<div class ="form-actions pleft80">
<%= f.submit nil, :class => 'btn btn-primary btn-large' %>
</div>
</div>
<% end %>
You don't need to instantiate or classify anything.
redirect_to #comment.commentable
If you can't do that then you will need to build a global helper module for it and include that into the controller.
module RouteHelpers
def comment_association_redirect_to(comment)
item = comment.commentable
case item.class.to_s
when 'Submission'
redirect_to submission_path(item)
end
end
end
And include it within the ApplicationController:
include RouteHelpers
Then you can call comment_association_redirect_to anywhere in your app (controllers and so on).
I stripped the nested routing out of the app and now it works fine and it's much simpler. Not sure I can think of a good reason to use nested routing when the views must relate the dependencies.
Related
I am trying to provide a way to generate a new object ("List") in one model with a new associated object in another ("Item") using a has_many through relationship (through "Groupings"). I was able to get the form working fine but can't figure out what I'm missing in order to finish the creation process correctly.
Rails v. 5.1.2, Ruby v. 2.4.1
lists_controller.rb
def new
#list = current_user.lists.new
3.times { #list.items.build }
end
def create
#list = current_user.lists.new(list_params)
respond_to do |format|
if #list.save
format.html { redirect_to #list, notice: 'List was successfully created.' }
format.json { render :show, status: :created, location: #list }
else
format.html { render :new }
format.json { render json: #list.errors, status: :unprocessable_entity }
end
end
end
private
def set_list
#list = List.find(params[:id])
end
def correct_user
#list = current_user.lists.find_by(id: params[:id])
redirect_to lists_path, notice: "Not authorized to edit this list"
if #list.nil?
end
def list_params
params.require(:list).permit(:title, {
item_attributes: [
:id, :title, :url
]})
end
items_controller.rb
def new
#item = Item.new
end
private
def set_item
#item = Item.find(params[:id])
end
def item_params
params.require(:item).permit(:title, :body, :url, :created,
:list_ids => [])
end
end
list.rb model
has_many :groupings, :dependent => :destroy
has_many :items, :through => :groupings
accepts_nested_attributes_for :items,
reject_if: ->(attrs) { attrs['title'].blank? || attrs['url'].blank? }
item.rb model
has_many :groupings, :dependent => :destroy
has_many :lists, :through => :groupings
validate :has_lists?
accepts_nested_attributes_for :lists
attr_writer :list_titles
after_save :assign_lists
def list_titles
#list_titles || lists.map(&:title).join(' ')
end
private
def assign_lists
if #list_titles
self.lists = #list_titles.split(/\,/).map do |title|
if title[0..0]==" "
title=title.strip
end
title=title.downcase
List.find_or_create_by_title(title)
end
end
end
def has_lists?
errors.add(:base, 'This item needs to be assigned to a list before it can be saved.') if self.lists.blank?
end
grouping.rb model
belongs_to :item
belongs_to :list
accepts_nested_attributes_for :item, :list
Lists Form
<%= form_with(model: list, local: true) do |f| %>
<% if list.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(list.errors.count, "error") %> prohibited this list from being saved:</h2>
<ul>
<% list.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :title %>
<%= f.text_field :title, id: :list_title %>
</div>
<div>
<p><strong>Items:</strong></p>
<%= f.fields_for :items do |item| %>
<div>
<%= item.label :title %>
<%= item.text_field :title %>
<%= item.label :url %>
<%= item.text_field :url %>
</div>
<% end %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Sample Console Output
Started POST "/lists" for 127.0.0.1 at 2017-09-19 13:12:53 -0700
Processing by ListsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Y6rszWVUXDIVymuoBkXwvkw1pVbyC6mffiWIZzr7PVd1NT9JJi6rD72k5Fh2qU5Q5tEd0qn6bFYMSJnz2TgjAA==", "list"=>{"title"=>"Websites", "items_attributes"=>{"0"=>{"title"=>"Google", "url"=>"www.google.com"}, "1"=>{"title"=>"Yahoo", "url"=>"www.yahoo.com"}, "2"=>{"title"=>"Bing", "url"=>"www.bing.com"}}}, "commit"=>"Create List"}
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 2], ["LIMIT", 1]]
Unpermitted parameter: :items_attributes
(0.1ms) BEGIN
SQL (0.9ms) INSERT INTO "lists" ("title", "created_at", "updated_at", "user_id") VALUES ($1, $2, $3, $4) RETURNING "id" [["title", "Websites"], ["created_at", "2017-09-19 20:12:53.458577"], ["updated_at", "2017-09-19 20:12:53.458577"], ["user_id", 2]]
(0.3ms) COMMIT
Redirected to http://localhost:3000/lists/24
Completed 302 Found in 7ms (ActiveRecord: 1.6ms)
I'm still learning, clearly - but after trying all kinds of related hints on this forum I couldn't figure this one out. Thanks for any help!
You're almost there but there is an error being reported in your console logs: Unpermitted parameter: :items_attributes.
Change item_attributes to items_attributes in your list_params:
def list_params
params.require(:list)
.permit(:title, items_attributes: [:id, :title, :url])
end
You have some mistakes in your syntax when you define your params. It should be like this: (items instead item and you don't need {})
def list_params
params.require(:list).permit(:title,
items_attributes: [:id, :title, :url])
end
I am using Rails 5 and everything at its newest stable versions. So I get the following :
You have your association set to required but it's missing.
Associations are set to required by default in rails 5 so if you want
to keep one empty you need to set optional:true on your association in
mode
This is great and I understand what is going on however for the life of me I cannot figure out how to get the parent model to save first so the user_id is translated the nested models record. I see the same answer above everywhere however no one explains a work around other than turning the default in the initializer from true to false. THIS DOES NOT SOLVE THE PROBLEM, because the record sure does save but it does not include the user_id.
Below is what I have for my code base, I would ask rather than responding with the above quote, could someone enlighten me on HOW to get the USER_ID field into the nested attributes while saving. I refuse to disable validation and manually handle the insertion, as this is not the ruby way and breaks from standards!
Thanks in advance for anyone who can answer this question directly and without vague explanations that digress from the ruby way of things!
###Models
#Users
class User < ApplicationRecord
has_one :profile, inverse_of: :user
accepts_nested_attributes_for :profile, allow_destroy: true
end
#Profiles
class Profile < ApplicationRecord
belongs_to :user, inverse_of: :profile
end
###Controller
class UsersController < ApplicationController
before_action :set_user, only: [:show, :edit, :update, :destroy]
# GET /users
# GET /users.json
def index
#users = User.all
end
# GET /users/1
# GET /users/1.json
def show
end
# GET /users/new
def new
#user = User.new
#user.build_profile
end
# GET /users/1/edit
def edit
#user.build_profile
end
# POST /users
# POST /users.json
def create
#user = User.new(user_params)
respond_to do |format|
if #user.save
format.html { redirect_to #user, notice: 'User was successfully created.' }
format.json { render :show, status: :created, location: #user }
else
format.html { render :new }
format.json { render json: #user.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /users/1
# PATCH/PUT /users/1.json
def update
respond_to do |format|
if #user.update(user_params)
format.html { redirect_to #user, notice: 'User was successfully updated.' }
format.json { render :show, status: :ok, location: #user }
else
format.html { render :edit }
format.json { render json: #user.errors, status: :unprocessable_entity }
end
end
end
# DELETE /users/1
# DELETE /users/1.json
def destroy
#user.destroy
respond_to do |format|
format.html { redirect_to users_url, notice: 'User was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_user
#user = User.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def user_params
params.require(:user).permit(:username, :password, :user_type_id, profile_attributes: [:user_id, :first_name, :middle_name, :last_name, :phone_number, :cell_number, :email])
end
end
##View
<%= form_for(#user) do |f| %>
<% if user.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(user.errors.count, "error") %> prohibited this user from being saved:</h2>
<ul>
<% user.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
<!--<li><%= debug f %></li>-->
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :username %>
<%= f.text_field :username %>
</div>
<div class="field">
<%= f.label :password %>
<%= f.text_field :password %>
</div>
<div class="field">
<% if params[:trainer] == "true" %>
<%= f.label :user_type_id %>
<%= f.text_field :user_type_id, :readonly => true, :value => '2' %>
<% else %>
<%= f.label :user_type_id %>
<%= f.text_field :user_type_id, :readonly => true, :value => '1' %>
<% end %>
</div>
<h2>Account Profile</h2>
<%= f.fields_for :profile do |profile| %>
<%#= profile.inspect %>
<div>
<%= profile.label :first_name %>
<%= profile.text_field :first_name %>
</div>
<div>
<%= profile.label :middle_name %>
<%= profile.text_field :middle_name %>
</div>
<div>
<%= profile.label :last_name %>
<%= profile.text_field :last_name %>
</div>
<div>
<%= profile.label :email %>
<%= profile.text_field :email %>
</div>
<div>
<%= profile.label :phone_number %>
<%= profile.telephone_field :phone_number %>
</div>
<div>
<%= profile.label :cell_phone %>
<%= profile.telephone_field :cell_number %>
</div>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<%= debug params %>
<%= debug user %>
<%= debug user.profile %>
<% end %>
UPDATE
For starters I have figured out that you need to include autosave: true to the relationship like so
class User < ApplicationRecord
has_one :profile, inverse_of: :user, autosave: true
accepts_nested_attributes_for :profile, allow_destroy: true
end
Then the parent record gets saved before the child. Now comes another gotcha that I am just not sure about and is odd when the form is submitted you will notice in the console output I pasted below that the INSERT INTO profiles statement includes the user_id column and the value of 1. It passees validation and looks like it runs properly from the output, however the user_id column in the profiles table is still null. I am going to keep digging, hopefuly one of my fellow rubyiests out there will see this and have some ideas on how to finish fixing this. I love Rails 5 improvements so far but it wouldn't be ROR without small interesting gotchas! Thanks again in advance!
Started POST "/users" for 192.168.0.31 at 2017-03-12 22:28:14 -0400
Cannot render console from 192.168.0.31! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by UsersController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"YA7kQnScvlIBy5OiT+BmOQ2bR7J00ANXId38FqNwX37Cejd+6faUyD3rMF4y0qJNKBUYGaxrRZqcLrXonL6ymA==", "user"=>{"username"=>"john", "password"=>"[FILTERED]", "user_type_id"=>"1", "profile_attributes"=>{"first_name"=>"john", "middle_name"=>"r", "last_name"=>"tung", "email"=>"thegugaru#gmail.com", "phone_number"=>"8033207677", "cell_number"=>"8033207677"}}, "commit"=>"Create User"}
(0.1ms) BEGIN
SQL (0.3ms) INSERT INTO `users` (`username`, `password`, `user_type_id`, `created_at`, `updated_at`) VALUES ('john', '0000', 1, '2017-03-13 02:28:14', '2017-03-13 02:28:14')
SQL (0.4ms) INSERT INTO `profiles` (`user_id`, `email`, `first_name`, `middle_name`, `last_name`, `phone_number`, `cell_number`, `created_at`, `updated_at`) VALUES (1, 'thegu#gmail.com', 'john', 'r', 'tung', '8033207677', '8033207677', '2017-03-13 02:28:14', '2017-03-13 02:28:14')
(10.8ms) COMMIT
Redirected to http://192.168.0.51:3000/users/1
Completed 302 Found in 24ms (ActiveRecord: 11.5ms)
Ok, I am answering my own question because I know many people are struggling with this and I actually have the answer and not a vague response to the documentation.
First we will just be using a one to one relationship for this example. When you create your relationships you need to make sure that the parent model has the following
inverse_of:
autosave: true
accepts_nested_attributes_for :model, allow_destroy:true
Here is the Users model then I will explain,
class User < ApplicationRecord
has_one :profile, inverse_of: :user, autosave: true
accepts_nested_attributes_for :profile, allow_destroy: true
end
in Rails 5 you need inverse_of: because this tells Rails that there is a relationship through foreign key and that it needs to be set on the nested model when saving your form data.
Now if you were to leave autosave: true off from the relationship line you are left with the user_id not saving to the profiles table and just the other columns, unless you have validations off and then it won't error out it will just save it without the user_id.
What is going on here is autosave: true is making sure that the user record is saved first so that it has the user_id to store in the nested attributes for the profile model.
That is it in a nutshell why the user_id was not traversing to the child and it was rolling back rather than committing.
Also one last gotcha is there are some posts out there telling you in your controller for the edit route you should add #user.build_profile like I have in my post. DO NOT DO IT THEY ARE DEAD WRONG, after assessing the console output it results in
Started GET "/users/1/edit" for 192.168.0.31 at 2017-03-12 22:38:17 -0400
Cannot render console from 192.168.0.31! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by UsersController#edit as HTML
Parameters: {"id"=>"1"}
User Load (0.4ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1
Profile Load (0.5ms) SELECT `profiles`.* FROM `profiles` WHERE `profiles`.`user_id` = 1 LIMIT 1
(0.1ms) BEGIN
SQL (0.5ms) UPDATE `profiles` SET `user_id` = NULL, `updated_at` = '2017-03-13 02:38:17' WHERE `profiles`.`id` = 1
(59.5ms) COMMIT
Rendering users/edit.html.erb within layouts/application
Rendered users/_form.html.erb (44.8ms)
Rendered users/edit.html.erb within layouts/application (50.2ms)
Completed 200 OK in 174ms (Views: 98.6ms | ActiveRecord: 61.1ms)
If you look it is rebuilding the profile from scratch and resetting the user_id to null for the record that matches the current user you are editing.
So be very careful of this as I have seen tons of posts making this suggestion and it cost me DAYS of research to find a solution!
I'm trying to figure out why my form is submitting wrong. I'm trying to implement nested comments under posts and my form renders but submits to the wrong action. I've tried a few things but cannot get it to work. I can create a comment via the rails console but not through my form. I was going through a railscast where he uses a older version of rails. I figured the main difference would be strong parameters but it doesn't seem to be working and I can't figure out what I'm missing here. Thanks.
This is my log
Started GET "/posts/1?utf8=%E2%9C%93&authenticity_token=PJmmRV6hnY%2Bgm4cVe5LSdALHezbI3ehMkud0yYTaA%2FQ%3D&comment%5Bname%5D=Mark&comment%5Bemail%5D=hustada80%40gmail.com&comment%5Bcontent%5D=this+is+a+comment&commit=Create+Comment" for 127.0.0.1 at 2014-10-08 14:34:53 -0500
Processing by PostsController#show as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"PJmmRV6hnY+gm4cVe5LSdALHezbI3ehMkud0yYTaA/Q=", "comment"=>{"name"=>"Mark", "email"=>"hustada80#gmail.com", "content"=>"this is a comment"}, "commit"=>"Create Comment", "id"=>"1"}
Post Load (0.2ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT 1 [["id", 1]]
Rendered comments/_form.html.erb (4.2ms)
Rendered comments/_comments.html.erb (0.1ms)
Rendered posts/show.html.erb within layouts/application (53.1ms)
Rendered layouts/_header.html.erb (0.5ms)
Rendered layouts/_footer.html.erb (0.1ms)
Completed 200 OK in 524ms (Views: 519.7ms | ActiveRecord: 0.2ms)
Here is my form ( a partial _form)
<div class="well">
<h4>Leave a comment</h4>
<form role="form" class="clearfix">
<%= form_for([#commentable, #comment]) do |f| %>
<div class="form-group">
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :email %>
<%= f.text_field :email, class: 'form-control', required: true %>
</div>
<div class="form-group">
<%= f.label :content %>
<%= f.text_area :content, class: 'form-control', required: true %>
</div>
<%= f.submit class: 'btn btn-primary' %>
<% end %>
</div>
Posts controller
class PostsController < ApplicationController
def new
#post = Post.new
end
def show
#post = Post.find(params[:id])
#commentable = #post
#comments = #commentable.comments
#comment = Comment.new
end
def index
#post = Post.all
#posts = Post.order('created_at DESC')
###posts_by_month = Post.find(:all, :order => 'created_at DESC').group_by { |post| post.created_at.strftime("%B %Y") }
end
def month_count
#posts_by_month = Post.find(:all, :order => 'created_at DESC').group_by { |post| post.created_at.strftime("%B %Y") }
end
def create
#post = Post.new(post_params)
#post.save
redirect_to #post
end
private
def post_params
params.require(:post).permit(:title, :text)
end
Comments Controller
class CommentsController < ApplicationController
before_action :load_commentable
def index
#comments = #commentable.comments
end
def new
#comment = #commentable.comments.new
end
def create
#comment = #commentable.comments.new(comments_params)
if #comment.save
redirect_to #commentable, notice: "Comment created."
else
render :new
end
end
def destroy
end
private
# def load_commentable
# resource, id = request.path.split('/')[1, 2]
# #commentable = resource.singularize.classify.constantize.find(id)
# end
def load_commentable
klass = [Post].detect { |c| params["#{c.name.underscore}_id"]}
#commentable = klass.find(params["#{klass.name.underscore}_id"])
end
def comments_params
params.require(:comment).permit(:content, :email, :name)
end
end
routes
Rails.application.routes.draw do
root 'posts#index'
get "sign_up" => "users#new", :as => "sign_up"
get "/log-in" => "sessions#new"
post "/log-in" => "sessions#create"
get "/log-out" => "sessions#destroy", as: :log_out
resources :posts do
resources :comments
end
resources :users
resources :sessions
Try something like
<%= form_for([#commentable, #comment], url: post_comments_path, method: :post) do |f| %>
I'm trying to use acts as message able gem and I'm following their example controller
SOLVED See Answer
I keep getting this error undefined method `send_message' for nil:NilClass when trying to send a message in the view
How should I adjust my code?
Thanks
View (Form)
<%= simple_form_for ActsAsMessageable::Message.new, :url => messages_path, :method => :post do |f| %>
<%= f.hidden_field :to, value: #gear.user.email %>
<%= f.input :body %>
<%= f.input :topic %>
<%= f.button :submit, class: 'btn' %>
<% end %>
User Model
class User < ActiveRecord::Base
acts_as_messageable :table_name => "messages", # default 'messages'
:required => [:topic, :body], # default [:topic, :body]
:class_name => "ActsAsMessageable::Message", # default "ActsAsMessageable::Message",
:dependent => :nullify # default :nullify
end
Messages Controller
class MessagesController < ApplicationController
def new
#message = ActsAsMessageable::Message.new
end
def create
#to = User.find_by_email(params[:acts_as_messageable_message][:to])
current_user.send_message(#to, params[:acts_as_messageable_message][:topic], params[:acts_as_messageable_message][:body])
end
end
Development Log
Started POST "/messages" for 127.0.0.1 at 2012-11-15 07:23:40 -0600
Processing by MessagesController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"OqaDOP6PldbFVXWPZyijn+887Ym/fDsU0oqzVrL0rQA=", "acts_as_messageable_message"=>{"to"=>"xyz#test.com", "body"=>"test", "topic"=>"test"}, "commit"=>"Create Message"}
[1m[35mUser Load (0.5ms)[0m SELECT `users`.* FROM `users` WHERE `users`.`email` = 'xyz#test.com' LIMIT 1
Completed 500 Internal Server Error in 3ms
NoMethodError (undefined method `send_message' for nil:NilClass):
app/controllers/messages_controller.rb:29:in `create
'
From the error message and provided code seems like your params obj is not formated like you think. Give params[:acts_as_messageable_message] a try instead of [:message]. If that doesn't work check the log to see what is being passed in as params.
I ended up getting it working. The problem ultimately was the gem was using the User model and was expecting the controller to be the User controller not another controller called "Messages". So I simply moved my actions into my Users controller, added the routes and changed the view path, and it now works. #Alex.Bullard thanks for the help.
I'm posting my edits below:
Controller Change
class UsersController < ApplicationController
respond_to :html, :json
def new_message
#message = ActsAsMessageable::Message.new
end
def create_message
#to = User.find_by_email(params[:acts_as_messageable_message][:to])
current_user.send_message(#to, params[:acts_as_messageable_message][:topic], params[:acts_as_messageable_message][:body])
redirect_to :back, notice: "Message sent to Owner"
end
end
View
<%= simple_form_for ActsAsMessageable::Message.new, :url => create_message_users_path, :method => :post do |f| %>
<%= f.hidden_field :to, value: #gear.user.email %>
<%= f.input :body %>
<%= f.input :topic %>
<%= f.button :submit, class: 'btn' %>
<% end %>
Routes
resources :users, :except => [ :create, :new ] do
get "new_message", :on => :collection
post "create_message", :on => :collection
resources :store
end
I'm new to rails.
And to web development at all.
Sorry if some questions might seem dumb.
Trying to follow this screen cast - http://emersonlackey.com/screencasts/rails-3-with-paperclip.mov
But stopped at the problem - when i try to upload an image i get the following error :
ActiveRecord::UnknownAttributeError in PostsController#update
Unknown attribute: image
altough post_controller.rb seems ok (checked many times - it is the same as https://github.com/Emerson/Multiple-File-Uploads-with-Paperclip-and-Rails-3) :
Tried googlin of course, but didn't find anything.
Has anyone been trough this tutorial and had this problem ?
Problem fixed, the _form code, was incorrect!
I had to change:
<%= f.fields_for :assets do |asset| %>
to
<%= f.fields_for :assets do |asset_fields| %>
and
<%= asset.file_field :image %>
to
<%= asset_fields.file_field :asset %>
And it worked.
The reasons was quite silly, i just didn't the watch the screencast till the end, because I stopped at the middle - when the problem showed-up, and spent my whole attention googling for the solution.
Beginners mistake!
Posts model:
class Post < ActiveRecord::Base
attr_accessible :title, :content, :assets_attributes
has_many :assets
accepts_nested_attributes_for :assets, :allow_destroy => true
end
Assets model:
class Asset < ActiveRecord::Base
belongs_to :post
has_attached_file :asset, :styles => { :large => "640x480", :medium=>"300x300>",
:thumb => "100x100>" }
end
Post controller:
class PostsController < ApplicationController
def index
#posts = Post.all
end
def show
#post = Post.find(params[:id])
end
def new
#post = Post.new
5.times { #post.assets.build }
end
def create
#post = Post.new(params[:post])
if #post.save
redirect_to #post, :notice => "Successfully created post."
else
render :action => 'new'
end
end
def edit
#post = Post.find(params[:id])
5.times { #post.assets.build }
end
def update
#post = Post.find(params[:id])
if #post.update_attributes(params[:post])
redirect_to #post, :notice => "Successfully updated post."
else
render :action => 'edit'
end
end
def destroy
#post = Post.find(params[:id])
#post.destroy
redirect_to posts_url, :notice => "Successfully destroyed post."
end
end
_form:
<%= form_for #post, :html => { :multipart => true } do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :title %><br />
<%= f.text_field :title %>
</p>
<p>
<%= f.label :content %><br />
<%= f.text_area :content %>
</p>
<%= f.fields_for :assets do |asset| %>
<% if asset.object.new_record? %>
<%= asset.file_field :image %>
<% end %>
<% end %>
<p><%= f.submit %></p>
<% end %>
Model index:
<% title "Posts" %>
<p><%= link_to "New Post", new_post_path %></p>
<hr />
<% for post in #posts %>
<div class="post">
<h2><%= link_to post.title, post%></h2>
<p class="content">
<%= post.content.html_safe %>
<br /><br />
<%= link_to "Edit", edit_post_path(post) %> | <%= link_to "Destroy", post,
:confirm => 'Are you sure?', :method => :delete %>
</p>
</div>
<% end %>
The error:
Started PUT "/posts/1" for 127.0.0.1 at Tue Sep 20 11:00:52 +0300 2011
Processing by PostsController#update as HTML
Parameters: {"commit"=>"Update Post", "post"=>{"title"=>"AAAAA", "content"=>"T
he enormous success of DropBox clearly shows that there's a huge need for simple
and fast file sharing.\r\n\r\nOur app will have the following features:\r\n\r\n
simple user authentication\r\n upload files and save them in Amazon S3\r\
n create folders and organize\r\n share folders with other users\r\n\r\nTh
roughout the tutorial, I will point out different ways that we can improve our a
pp. Along the way, we'll review a variety of concepts, including Rails scaffoldi
ng and AJAX.", "assets_attributes"=>{"0"=>{"image"=>#<ActionDispatch::Http::Uplo
adedFile:0x436fda0 #tempfile=#<File:C:/DOCUME~1/emve/LOCALS~1/Temp/RackMultipart
20110920-8900-zgz1ej-0>, #headers="Content-Disposition: form-data; name=\"post[a
ssets_attributes][0][image]\"; filename=\"02.jpg\"\r\nContent-Type: image/jpeg\r
\n", #content_type="image/jpeg", #original_filename="02.jpg">}}}, "authenticity_
token"=>"WHsbBak0O2xYBFe/h82+4/5aV2VPzHDdXcgb4QYmC4A=", "utf8"=>"Ō£ō", "id"=>"1"
}
←[1m←[35mPost Load (0.0ms)←[0m SELECT "posts".* FROM "posts" WHERE "posts"."i
d" = ? LIMIT 1 [["id", "1"]]
Completed 500 Internal Server Error in 63ms
ActiveRecord::UnknownAttributeError (unknown attribute: image):
app/controllers/posts_controller.rb:32:in `update'
Rendered C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/actionpack-3.1.0/lib
/action_dispatch/middleware/templates/rescues/_trace.erb (0.0ms)
Rendered C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/actionpack-3.1.0/lib
/action_dispatch/middleware/templates/rescues/_request_and_response.erb (0.0ms)
Rendered C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/actionpack-3.1.0/lib
/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/lay
out (46.9ms)