I'm not sure what is happening here. I've checked everything here but I'm getting this double file upload field:
Even if I use only one field it still renders two images.
_form.html.erb:
<%= nested_form_for #home, :html=>{:multipart => true } do |f| %>
<% if #home.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#home.errors.count, "error") %> prohibited this blog from being saved:</h2>
<ul>
<% #home.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<p>
<%= f.label :body %><br />
<%= f.text_area :body %>
</p>
<p>
<%= f.fields_for :attachments do |attachment_form| %>
<%= attachment_form.file_field :file %>
<br /><br />
<%= attachment_form.link_to_remove "Remove this attachment" %>
<% for attachment in #home.attachments %>
<div class="show_image">
<%= image_tag attachment.file_url(:thumb).to_s %>
</div>
<% end %>
<% end %>
<br /><br />
<%= f.link_to_add "Add attachmnt", :attachments %>
</p>
<br /><br />
<p><%= f.submit %></p>
<br /><br />
<% end %>
Models:
attachment.rb:
class Attachment < ActiveRecord::Base
attr_accessible :description, :file
belongs_to :attachable, :polymorphic => true
mount_uploader :file, FileUploader
end
home.rb:
class Home < ActiveRecord::Base
attr_accessible :body, :attachments_attributes
has_many :attachments, :as => :attachable
accepts_nested_attributes_for :attachments, :allow_destroy => true
end
homes_controller.rb:
class HomesController < ApplicationController
# GET /homes
# GET /homes.json
def index
#homes = Home.all
respond_to do |format|
format.html # index.html.erb
format.json { render :json => #homes }
end
end
# GET /homes/1
# GET /homes/1.json
def show
#home = Home.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render :json => #home }
end
end
# GET /homes/new
# GET /homes/new.json
def new
#home = Home.new
respond_to do |format|
format.html # new.html.erb
format.json { render :json => #home }
end
end
# GET /homes/1/edit
def edit
#home = Home.find(params[:id])
end
# POST /homes
# POST /homes.json
def create
#home = Home.new(params[:home])
respond_to do |format|
if #home.save
format.html { redirect_to #home, :notice => 'Home was successfully created.' }
format.json { render :json => #home, :status => :created, :location => #home }
else
format.html { render :action => "new" }
format.json { render :json => #home.errors, :status => :unprocessable_entity }
end
end
end
# PUT /homes/1
# PUT /homes/1.json
def update
#home = Home.find(params[:id])
respond_to do |format|
if #home.update_attributes(params[:home])
format.html { redirect_to #home, :notice => 'Home was successfully updated.' }
format.json { head :no_content }
else
format.html { render :action => "edit" }
format.json { render :json => #home.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /homes/1
# DELETE /homes/1.json
def destroy
#home = Home.find(params[:id])
#home.destroy
respond_to do |format|
format.html { redirect_to homes_url }
format.json { head :no_content }
end
end
end
How do I stop this from happening?
Any help with this would be great, I've been looking at this for hours!
My first guess would be bad data created while getting this set up. Maybe there are previous attachments on the Home object that are not being displayed correctly, but still exist in the database. Does the same problem happen if you create a new Home and attempt to add attachments?
The fields_for block is creating a button and a display of each photo for each attachment it's being passed. I think that's why you're seeing the duplication. The fields_for is an iterator on attachments with a duplicate iterator inside it (for attachment in #home.attachments).
Related
I am trying to make an app in Rails 4.
I have 3 models: Project, Project_Question, Project_Answer
The associations are:
Project:
has_many :project_questions
and accepts nested attributes for project questions.
Project Question:
belongs_to :project
has_one :project_answer
and accepts nested attributes for Project Answers.
My routes are nested as follows:
resources :projects do
resources :project_questions do
resources :project_answers
end
end
In my Project Questions partial, I want a link to answer the question. I've tried:
<%= link_to 'Answer this question', new_project_project_question_project_answer_path(:project_question_id => project_question.id) %>
I have a route with that name in my routes file, but I'm getting this error message:
undefined local variable or method `project_question' for #<#<Class:0x0000010742b9d8>:0x0000010f810b68>
What should go in the brackets?
View:
<div class="containerfluid">
<div class="row">
<div class="col-md-10 col-md-offset-1">
<% #project.project_questions.each do |singleQuestion| %>
<div class="categorytitle">
<%= singleQuestion.title %>
</div>
<div class="generaltext">
<%= singleQuestion.try(:content) %>
</div>
<span class="editproject">
<% if current_user.id == #project.creator_id %>
<%= link_to 'Answer this question', new_project_project_questions_project_answer_path(:project_question_id => project_question.id) %>
<% end %>
</span>
<% end %>
</div>
</div>
</div>
Project Question controller:
class ProjectQuestionsController < ApplicationController
before_action :set_project_question, only: [:show, :edit, :update, :destroy]
# GET /project_questions
# GET /project_questions.json
def index
#project_questions = ProjectQuestion.all
end
# GET /project_questions/1
# GET /project_questions/1.json
def show
end
# GET /project_questions/new
def new
#project_question = ProjectQuestion.new
#project = Project.find(params[:project_id])
# #project_id = params[:project_id]
#project_question.project_answers[0] = ProjectAnswer.new
end
# GET /project_questions/1/edit
def edit
end
# POST /project_questions
# POST /project_questions.json
def create
#project_question = ProjectQuestion.new(project_question_params)
#project_question.project_id = project_question_params[:project_id]
respond_to do |format|
if #project_question.save
format.html { redirect_to project_url(Project.find(project_question_params[:project_id])), notice: 'Project question was successfully created.' }
format.json { render action: 'show', status: :created, location: #project_question }
else
format.html { render action: 'new' }
format.json { render json: #project_question.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /project_questions/1
# PATCH/PUT /project_questions/1.json
def update
respond_to do |format|
if #project_question.update(project_question_params)
format.html { redirect_to #project_question, notice: 'Project question was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #project_question.errors, status: :unprocessable_entity }
end
end
end
# DELETE /project_questions/1
# DELETE /project_questions/1.json
def destroy
#project_question.destroy
respond_to do |format|
format.html { redirect_to project_questions_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_project_question
#project_question = ProjectQuestion.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def project_question_params
params[:project_question].permit(:id, :title, :content, :project_id, :user_id,
project_answer_atttibutes: [:id, :answer, :project_question_id, :user_id]
)
end
end
When you run rake routes, you will find this one
new_project_project_question_project_answer GET /projects/:project_id/project_questions/:project_question_id/project_answers/new(.:format) project_answers#new
That means it requires :project_id and :project_question_id as keys.
This should work
<%= link_to 'Answer this question', new_project_project_question_project_answer_path(:project_id => #project.id, :project_question_id => singleQuestion.id) %>
Notice new_project_project_question_project_answer_path not new_project_project_questions_project_answer_path
Your link_to should be something below
<%= link_to 'Answer this question', new_project_project_questions_project_answer_path(:project_id => #project.id, :project_question_id => singleQuestion.id) %>
View will look like below
<div class="containerfluid">
<div class="row">
<div class="col-md-10 col-md-offset-1">
<% #project.project_questions.each do |singleQuestion| %>
<div class="categorytitle">
<%= singleQuestion.title %>
</div>
<div class="generaltext">
<%= singleQuestion.try(:content) %>
</div>
<span class="editproject">
<% if current_user.id == #project.creator_id %>
<%= link_to 'Answer this question', new_project_project_questions_project_answer_path(:project_question_id => singleQuestion.id) %>
<% end %>
</span>
<% end %>
</div>
</div>
</div>
Check that params
def project_question_params
params[:project_question].permit(:id, :title, :content, :project_id, :user_id,
project_answer_atttibutes: [:id, :answer, :project_question_id, :user_id]
)
end
There is project_id
and you did not pass it in link_to
so it thwos new errormissing required keys: [:project_id]
I am building a book repository in rails and I need to be able to add an author inside a book create form that passes the author post into the list of authors once the book has been added. In the same book creation resource I have already created the has_many: authors in the book.rb file and in the author.rb file I have created the belongs_to: author and that works fine I can select the books that author might have created with the following setup:
book.rb
class Book < ActiveRecord::Base
has_attached_file :jacket_cover, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
validates_attachment_content_type :jacket_cover, :content_type => /\Aimage\/.*\Z/
validates :jacket_cover, :title, :synopsis, :body, presence: true
belongs_to :author
scope :available, ->{ where(available: true) }
scope :unavailable, ->{ where(available: [nil, false]) }
end
author.rb
class Author < ActiveRecord::Base
has_many :books
end
books_controller.rb
class BooksController < ApplicationController
before_action :set_book, only: [:show, :edit, :update, :destroy]
def index
#books = Book.all
end
def show
end
# GET /books/new
def new
#book = Book.new
end
# GET /books/1/edit
def edit
end
def create
#book = Book.new(book_params)
respond_to do |format|
if #book.save
format.html { redirect_to #book, notice: 'Book was successfully created.' }
format.json { render action: 'show', status: :created, location: #book }
else
format.html { render action: 'new' }
format.json { render json: #book.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if #book.update(book_params)
format.html { redirect_to #book, notice: 'Book was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #book.errors, status: :unprocessable_entity }
end
end
end
def destroy
#book.destroy
respond_to do |format|
format.html { redirect_to books_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_book
#book = Book.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def book_params
params.require(:book).permit(:title, :synopsis, :body, :jacket_cover)
end
end
authors_controller.rb
class AuthorsController < ApplicationController
before_action :set_author, only: [:show, :edit, :update, :destroy]
def index
#authors = Author.all
end
def show
end
def new
#author = Author.new
end
# GET /authors/1/edit
def edit
end
def create
#author = Author.new(author_params)
respond_to do |format|
if #author.save
format.html { redirect_to #author, notice: 'Author was successfully created.' }
format.json { render action: 'show', status: :created, location: #author }
else
format.html { render action: 'new' }
format.json { render json: #author.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if #author.update(author_params)
format.html { redirect_to #author, notice: 'Author was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #author.errors, status: :unprocessable_entity }
end
end
end
def destroy
#author.destroy
respond_to do |format|
format.html { redirect_to authors_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_author
#author = Author.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def author_params
params.require(:author).permit(:name, :biography, :books_ids => [] )
end
end
This allows me to create the books and the authors which is fine but I am now looking to nest the author create into my book create too.
form for book create
<%= simple_form_for(#book, :html => { :multipart => true }) do |f| %>
<%= f.error_notification %>
<div class="inputs">
<%= f.file_field :jacket_cover %>
<%= f.input :title %>
<%= f.input :synopsis %>
<%= f.input :body %>
</div>
<div class="actions">
<%= f.button :submit %>
</div>
<% end %>
form for author create
<%= simple_form_for(#author) do |f| %>
<%= f.error_notification %>
<div class="inputs">
<%= f.input :name %>
<%= f.association :books,
as: :check_boxes,
value_method: :id,
label: 'Books' %>
<%= f.input :biography %>
</div>
<div class="actions">
<%= f.button :submit %>
</div>
<% end %>
Is it possible to nest another resource into an already created resource so adding an author create inside a book create page?
Yes,you need accepts_nested_attributes_for and simple_fields_for helper provided for the simple_form_for(as you are using simple_form_for)
Step #1
In your Book model,you should add accepts_nested_attributes_for :author
Step #2
Modifying your new method of books_controller.rb
As you have belongs_to :author in your Book model,your new method of your BooksController would be
def new
#book = Book.new
#book.build_author #This is very important
end
Step #3
Your book_params method should be modified to
def book_params
params.require(:book).permit(:title, :synopsis, :body, :jacket_cover,author_attributes: [:name,:biography,..,..])
end
Step #4
Finally,your form for book create would be something like this
<%= simple_form_for(#book, :html => { :multipart => true }) do |f| %>
<%= f.error_notification %>
<div class="inputs">
<%= f.file_field :jacket_cover %>
<%= f.input :title %>
<%= f.input :synopsis %>
<%= f.input :body %>
</div>
<%= f.simple_fields_for :author do |a| %>
... author fields...
....................
<% end %>
<div class="actions">
<%= f.button :submit %>
</div>
<% end %>
Is it possible to nest another resource into an already created resource so adding an author create inside a book create page?
Yes.
You probably want a longer answer than that though.
You need to get nested attributes sorted out.
In Book model:
has_one :author, dependent: :destroy
accepts_nested_attributes_for :authors
In BooksController :new add line
#book = Author.new
#book.build_author
And in form for #book:
<%= form_for(#book, :html => { :multipart => true }) do |f| %>
<%= f.error_notification %>
<div class="inputs">
#book fields
</div>
<%= f.fields_for :author do |author| %>
#author fields
<%= f.button :submit %>
</div>
<% end %>
And don't forget to modify book_params method as shown in answer by Rich Peck.
Your question is somewhat verbose, but I'll detail what you need to know
--
Nested Objects
Is it possible to nest another resource into an already created resource so adding an author create inside a book create page?
Rails is built on top of Ruby (an object orientated language). This means Rails is object orientated too, and so if you wanted to create an author at the same time as creating a book (this is only applicable for create), you'll want to use the accepts_nested_attributes_for directive for your model:
#app/models/book.rb
Class Book < ActiveRecord::Base
has_one :author
accepts_nested_attributes_for :author
end
#app/controllers/books_controller.rb
Class BooksController < ApplicationController
def new
#book = Book.new
#book.build_author #-> this will be authors.build if multiple
end
def create
#book = Book.new(book_params)
#book.save
end
private
def book_params
params.require(:book).permit(:title, :synopsis, :body, author_attributes: [:biography])
end
end
This will allow you to create the following (using form_for for simplicity sake):
#app/views/books/new.html.erb
<%= form_for #book do |f| %>
<%= f.text_field :title %>
<%= f.text_field :synopsis %>
<%= f.text_field :body %>
<%= f.fields_for :author do |a| %>
<%= a.text_field :biography %>
<% end %>
<%= f.submit %>
<% end %>
This will create the book record & associated author record, too
I have just followed step by step this other question, but my app is still giving me some errors regarding tagging (rails 4)
Error that Im experiencing is: Desktop/hack/app/controllers/jacks_controller.rb:85: syntax error, unexpected end-of-input, expecting keyword_end end ^
I have already re-intended as suggested in the chat, but nothing changed.
Code for reference
I have no associations between my models, (in the link that I have referred, there are associations)
jack form
<%= form_for(#jack) do |f| %>
<% if #jack.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#jack.errors.count, "error") %> prohibited this jack from being saved: </h2>
<ul>
<% #jack.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :title %><br>
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :description %><br>
<%= f.text_area :description %>
</div>
<div class="field">
<%= f.label :tag_list, "Tags (separated by comma)" %><br>
<%= f.text_field :tag_list %>
</div>
<div class="field">
<%= f.label :picture %><br>
<%= f.text_field :picture %>
</div>
<div class="actions">
<%= f.submit %>
jack.rb
class Jack < ActiveRecord::Base
acts_as_taggable_on :tags
end
Routes
Rails.application.routes.draw do
get 'tagged/index'
root :to => redirect('/jacks')
get 'about' => "about#info"
get 'submit' => "jacks#create"
resources :jacks
match 'tagged', to: 'jacks#tagged', :as => 'tagged', via: 'get'
resources :users
jack active helper
module JacksHelper
include ActsAsTaggableOn::TagsHelper
end
And controller
class JacksController < ApplicationController
before_action :set_jack, only: [:show, :edit, :update, :destroy]
# GET /jacks
# GET /jacks.json
def index
if params [:tag]
#jacks = Jack.tagged_with(params[:tag])
else
#jacks = Jack.all
end
# GET /jacks/1
# GET /jacks/1.json
def show
end
# GET /jacks/new
def new
#jack = Jack.new
end
# GET /jacks/1/edit
def edit
end
# POST /jacks
# POST /jacks.json
def create
#jack = Jack.new(jack_params)
respond_to do |format|
if #jack.save
format.html { redirect_to #jack, notice: 'Jack was successfully created.' }
format.json { render :show, status: :created, location: #jack }
else
format.html { render :new }
format.json { render json: #jack.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /jacks/1
# PATCH/PUT /jacks/1.json
def update
respond_to do |format|
if #jack.update(jack_params)
format.html { redirect_to #jack, notice: 'Jack was successfully updated.' }
format.json { render :show, status: :ok, location: #jack }
else
format.html { render :edit }
format.json { render json: #jack.errors, status: :unprocessable_entity }
end
end
end
# DELETE /jacks/1
# DELETE /jacks/1.json
def destroy
#jack.destroy
respond_to do |format|
format.html { redirect_to jacks_url, notice: 'Jack was successfully destroyed.' }
format.json { head :no_content }
end
end
def tagged
if params[:tag].present?
#jacks = Jack.tagged_with(params[:tag])
else
#jacks = Jack.postall
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_jack
#jack = Jack.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def jack_params
params.require(:jack).permit(:title, :description, :picture, :tag_list)
end
end
index method is missing an 'end'
def index
if params[:tag]
#jacks = Jack.tagged_with(params[:tag])
else
#jacks = Jack.all
end
end
I'm currently building this website in college for fun. My hope is that it will help the education system. Anyways, I'm still trying to figure out rails. I just set up the devise gem with no problem. However, when I click post a new status it gives me this error:
NoMethodError in Statuses#new
Showing /Users/wyatt/Network/netbook/app/views/statuses/_form.html.erb where line #16 raised:
undefined method `user_name' for #<Status:0x00000101ec36d0>
Extracted source (around line #16):
13:
14: <div class="field">
15: <%= f.label :user_name %><br />
16: <%= f.text_field :user_name %>
17: </div>
18: <div class="field">
19: <%= f.label :content %><br />
So here's what my form looks like:
<%= form_for(#status) do |f| %>
<% if #status.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#status.errors.count, "error") %> prohibited this status from being saved:</h2>
<ul>
<% #status.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :user_name %><br />
<%= f.text_field :user_name %>
</div>
<div class="field">
<%= f.label :content %><br />
<%= f.text_area :content %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Here's my controller
class StatusesController < ApplicationController
# GET /statuses
# GET /statuses.json
def index
#statuses = Status.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #statuses }
end
end
# GET /statuses/1
# GET /statuses/1.json
def show
#status = Status.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: #status }
end
end
# GET /statuses/new
# GET /statuses/new.json
def new
#status = Status.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: #status }
end
end
# GET /statuses/1/edit
def edit
#status = Status.find(params[:id])
end
# POST /statuses
# POST /statuses.json
def create
#status = Status.new(params[:status])
respond_to do |format|
if #status.save
format.html { redirect_to #status, notice: 'Status was successfully created.' }
format.json { render json: #status, status: :created, location: #status }
else
format.html { render action: "new" }
format.json { render json: #status.errors, status: :unprocessable_entity }
end
end
end
# PUT /statuses/1
# PUT /statuses/1.json
def update
#status = Status.find(params[:id])
respond_to do |format|
if #status.update_attributes(params[:status])
format.html { redirect_to #status, notice: 'Status was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: #status.errors, status: :unprocessable_entity }
end
end
end
# DELETE /statuses/1
# DELETE /statuses/1.json
def destroy
#status = Status.find(params[:id])
#status.destroy
respond_to do |format|
format.html { redirect_to statuses_url }
format.json { head :no_content }
end
end
end
From looking at it, it seems you're wanting to append a user_name to a status
Error
Your error basically means you don't have a user_name column in your statuses data table. The basic fix will be to create a migration to add a user_name column to your statuses db:
$ rails generate migration AddUserNameToStatuses
#db/migrations/AddUserNameToStatuses.rb
class AddUserNameToStatuses < ActiveRecord::Migration
def change
add_column :statuses, :user_name, :string
end
end
$ rake db:migrate
Fix
I would actually ditch that, and do this:
#app/models/status.rb
Class Status < ActiveRecord::Base
belongs_to :user #-> you need user_id column in statuses db
delegate :name, to: :user, prefix: true
end
#app/models/user.rb
Class User < ActiveRecord::Base
has_many :statuses
end
This will allow you to remove the references to user_name from your form - as any #status will be associated with a user
You'd then be able to use the .delegate() method to call user_name in statuses
If I have a model...
class Post
include Mongoid::Document
field :link
field :title
field :synopsis
field :added_on, :type => Date
validates_presence_of :link
embeds_many :replies
references_one :topic
end
and
class Topic
include Mongoid::Document
field :category
referenced_in :post
end
What would I need to code in index.html.erb to access data in topic or to add a topic to post.
I tried post.topic but I get an undefined method error.
Thank you very much.
Edit:
Here is the index.html code
<div id="post">
<% #posts.each do |post| %>
<div class="title_container">
<%= link_to post.title, post.link %> || <%= link_to 'Edit', edit_post_path(post) %> || <%= post.topic %>
</div>
<% end %>
<br />
<h2>Topics<h2>
<% for topic in #post.topics %>
<h3><%= topic.category %></h3>
<% end %>
</div>
here is the posts_controller
class PostsController < ApplicationController
# GET /posts
# GET /posts.xml
def index
#posts = Post.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => #posts }
end
end
# GET /posts/1
# GET /posts/1.xml
def show
#post = Post.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => #post }
end
end
# GET /posts/new
# GET /posts/new.xml
def new
#post = Post.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => #post }
end
end
# GET /posts/1/edit
def edit
#post = Post.find(params[:id])
end
# POST /posts
# POST /posts.xml
def create
#post = Post.new(params[:post])
respond_to do |format|
if #post.save
format.html { redirect_to(#post, :notice => 'Post was successfully created.') }
format.xml { render :xml => #post, :status => :created, :location => #post }
else
format.html { render :action => "new" }
format.xml { render :xml => #post.errors, :status => :unprocessable_entity }
end
end
end
# PUT /posts/1
# PUT /posts/1.xml
def update
#post = Post.find(params[:id])
respond_to do |format|
if #post.update_attributes(params[:post])
format.html { redirect_to(#post, :notice => 'Post was successfully updated.') }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => #post.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /posts/1
# DELETE /posts/1.xml
def destroy
#post = Post.find(params[:id])
#post.destroy
respond_to do |format|
format.html { redirect_to(posts_url) }
format.xml { head :ok }
end
end
end
Edit:
I am also adding the relevant _form.html.erb code.
<div class="field">
<%= f.label :topic_id %>
<%= f.collection_select :topic, Post.topic, :id, :category, :prompt => "Select a Topic" %>
</div>
Edit:
Updated to 2.0.0.rc.7 still can't get it.
Tried the key method in the railscast video (http://railscasts.com/episodes/238-mongoid) just for fun. I get a "BSON::InvalidObjectId in PostsController#update" error.
I'm guessing a topic has many posts? If you want a referenced association you need to change it to this.
class Post
#...
referenced_in :topic
end
class Topic
#...
references_many :posts
end
Then try changing your collection_select line to this:
<%= f.collection_select :topic_id, Topic.all, :id, :category, :prompt => "Select a Topic" %>
Your post.rb file has a references_one :topic, but in your index view, you're doing for topic in #post.topics, implying that a post can have many topics. Either you need to change your model to say references_many :topics or change your view to work with only having one topic per post.
#user593120
Did you tried this in your index.html.erb
<div id="post">
<% #posts.each do |post| %>
<div class="title_container">
<%= link_to post.title, post.link %> || <%= link_to 'Edit', edit_post_path(post) %> || <%= post.topic %>
</div>
<% end %>
<br />
<h2>Topics<h2>
<% #posts.each do |post| %>
<h3><%= post.topic.category if post.topic %></h3>
<% end %>
</div>