I'm creating a simple discussion board in Rails. Every new Topic creates also a first Reply that includes the content. This is my current schema.
Topic
> title:string
> user_id: integer
has_many :replies
accepts_nested_attributes_for :replies
Reply
> topic_id: integer
> user_id: integer
> content: text
belongs_to :topic
The current topics/_form.html.haml is something like this
= form_for #topic fo |f|
= f.text_field :title
= f.fields_for :replies
= reply.text_area :content
The problem is when trying to edit a Topic, I see all the list of replies as editable since it's iterating the fields_for :replies field in the form partial. I should only see the first one.
What would be a convenient way to limit this iteration to its current first available reply only while also building a new one if a topic is new?
I ended up with something like this that works but I guess there should be a better way.
# Topic model
has_one :owner_reply, class_name: 'Reply'
accepts_nested_attributes_for :owner_reply
# Form partial view
= form_for #topic fo |f|
- reply_resource = (#topic.new_record? ? :replies : :owner_reply)
= f.text_field :title
= f.fields_for :replies
= reply.text_area :content
These are the full TopicsController#create and update actions.
def create
#board = Board.find(params[:board_id])
#topic = #board.topics.new(topic_params)
#topic.user_id = current_user.id
#topic.replies.each { |reply| reply.user_id = current_user.id }
if #topic.save
respond_to do |format|
format.html { redirect_to topic_path(#topic) }
end
else
render :new
end
end
def update
#topic = Topic.find(params[:id])
if #topic.update_attributes(topic_params)
respond_to do |format|
format.html { redirect_to topic_path(#topic) }
end
else
render :edit
end
end
I would use a scoped association, the same way you are using :owner_reply but adding a scope to limit to the first record, you can also add a order to it if you need
class Topic
has_many :replies
has_many :first_replies, -> { first }, class_name: 'Reply'
accepts_nested_attributes_for :replies
accepts_nested_attributes_for :first_replies
And in your view
= form_for #topic fo |f|
...
= f.fields_for :first_replies
= reply.text_area :content
Create a class method on Topic that returns the first Reply:
class Topic
accepts_nested_attributes_for :first_reply
def self.first_reply
self.replies.first
end
# ...
end
Then call the class method in fields_for.
Related
I am trying to use a nested resource in rails to create a new record. I am attempting to use of /events/id/entries/new where there is a
<%= form_for([#event, #entry]) do |f| %>
on the new page.
My models are:
class Entry < ApplicationRecord
belongs_to :user
belongs_to :event
class User < ApplicationRecord
has_many :entries, dependent: :destroy
class Event < ApplicationRecord
has_many :entries, dependent: :destroy
The controller is:
def new
#event = Event.find(params[:event_id])
#entry = #event.entries.build
end
def create
#user = current_user
#entry = Entry.new(user_id: #user.id, event_id: :event_id , course: :entry.course , siCard: :entry.siCard)
if #entry.save
redirect_to #user
flash.now[:info] = "Event Created"
else
render '/create'
flash.now[:danger] = "Somthing went wrong"
end
end
But the record does not save NoMethodError in EntriesController#createdespite it saying that the parameters passed are.
"utf8"=>"✓",
"entry"=>{"course"=>"Orange", "siCard"=>"23232323"},
"commit"=>"Enter",
"event_id"=>"2"}
How do I modify the create controller to save the record?
Give this a go:
#entry = Entry.new(
user_id: current_user.id,
event_id: params[:event_id],
course: params[:entry][:course],
siCard: params[:entry][:siCard]
)
You don't need to #user = current_user, just use current_user.id.
event_id: :event_id is just going to set event_id to a symbol. You want to access the params value, I am guessing. So:
event_id: params[:event_id]
Here:
course: :entry.course
you're trying to call the course method on the :entry symbol. What?!?
Instead:
course: params[:entry][:course]
Same thing with siCard.
BTW, siCard is not very Ruby-ish. Rather, you should do si_card.
I'm a newbie in rails and trying to implement image uploading to ftp with 'carrierwave-ftp' gem. For image uploading, I have two controllers. First one is 'events_controller' while the second one is 'events_pictures_controller'.
Pictures are getting uploading to ftp. But the problem is that when I'm deleting a single picture, it is destroying the entire event. Please help!
Here is my Events Model:
class Event < ActiveRecord::Base
has_many :event_pictures, dependent: :destroy
accepts_nested_attributes_for :event_pictures, allow_destroy: true
validates_presence_of :name, :date
end
Here is my EventPictures Model:
class EventPicture < ActiveRecord::Base
mount_uploader :picture_title, EventPicturesUploader
validates_presence_of :picture_title
belongs_to :event, dependent: :destroy
end
Events Controller:
def index
#events = Event.all.order('date DESC')
end
def show
#event_pictures = #event.event_pictures.all
end
def new
#event = Event.new
#event_picture = #event.event_pictures.build
end
def edit
end
def create
#event = Event.new(event_params)
respond_to do |format|
if #event.save
params[:event_pictures]['picture_title'].each do |a|
#event_picture = #event.event_pictures.create!(:picture_title => a, :event_id => #event.id)
end
format.html { redirect_to #event, notice: 'Event was successfully created.' }
else
format.html { render :new }
end
end
end
def destroy
#event = Event.find params[:id]
#event.destroy
redirect_to events_url
end
private
def set_event
#event = Event.find(params[:id])
end
def event_params
params.require(:event).permit(:name, :date, event_pictures_attributes: [:id, :event_id, :picture_title])
end
This is the Destroy method in EventPictures Controller
def destroy
#event_picture = EventPicture.find params[:id]
#event_picture.destroy
redirect_to "events_url"
end
Meanwhile in the events.show.html.erb, I have this:
<% #event_pictures.each do |p| %>
<%= link_to image_tag(p.picture_title_url, :class => 'event-img'), image_path(p.picture_title_url) %>
<%= link_to 'Delete', p, method: :delete, data: { confirm: "Are you sure?" } %>
<% end %>
In your EventPicture model you have dependent: :destroy on the association which means that when the picture will deleted the corresponding events too. So just edit the association and make it:
belongs_to :event
And you have dependent destroy on the Event model so when a event will be deleted the corresponding pictures too will get deleted which is correct.
Hope this helps.
I believe your error lies with this line
belongs_to :event, dependent: :destroy
This is telling the EventPicture model to delete its parent model Event when it is deleted.
Replace with
belongs_to :event
I am trying to store client_id in join table: clients_orders after submitting the form below.
I set the tables in this way so I can look up all the orders a client has made.
I am using rails 4 with devise and simple form.
models
class Order < ActiveRecord::Base
belongs_to :user
#has_and_belongs_to_many :clients
belongs_to :clients #solution
end
class Client < ActiveRecord::Base
#has_and_belongs_to_many :orders
as_many :orders, dependent: :destroy #solution
end
orders form
<%= simple_form_for(#order) do |f| %>
<%= f.error_notification %>
<%= f.association :client, collection: Client.all, label_method: :name, value_method: :id, prompt: "Choose a Client" } %>
<%= etc... %>
<%= f.submit %>
<% end %>
with the current code above, the join table clients_orders does not update
create_table "clients_orders", id: false, force: true do |t|
t.integer "client_id"
t.integer "order_id"
end
order controller
class OrdersController < ApplicationController
# GET /orders/new
def new
#order = Order.new
end
# POST /orders
# POST /orders.json
def create
#order = Order.new(order_params)
#order.user_id = current_user.id
respond_to do |format|
if #order.save
format.html { redirect_to #order, notice: 'Order was successfully created.' }
format.json { render :show, status: :created, location: #order }
else
format.html { render :new }
format.json { render json: #order.errors, status: :unprocessable_entity }
end
end
end
# Never trust parameters from the scary internet, only allow the white list through.
def order_params
params.require(:order).permit(:code, :client_id, :user_id, :memo, :status, items_attributes: [:id, :name, :_destroy])
end
end
For future reference:
this is a simple one-to-many relationship. All you have to do to access a client's orders is to set up the has_many :orders in the User model and belongs_to :user in Order model. Then you can use collection methods like current_user.orders and it will get all of that specific user's orders for you. Just assign it to the user with #order = current_user.orders.build(:order_params)
You aren't whitelisting the correct parameters in your create action.
When dealing with has_and_belongs_to_many associations, you're dealing with multiple objects on each side, so the attributes you're whitelisting are plural, not singular.
You need to be whitelisting client_ids, not client_id.
Also, I'm pretty sure your form is wrong. You have it setup as though client is a has_one relationship. I think you want the plural version there as well.
<%= f.association :clients, #...
# ^----- add an 's'
If you really intended for the form to model a singular relationship, then you'll need to massage the data somewhere before saving your model. Here's one way to do it:
def create
#order = Order.new(order_params)
#order.client_ids << params[:order][:client_id]
#order.user_id = current_user.id
# save and respond...
end
If you go this route, then just remove :client_id from your parameters whitelist rather than pluralizing it.
#post.comments.all is clear. and i dont see any errors after i send form. When i click "Submit" i sent to posts/id/comments, but
my routes.rb
resources :posts do
resources :comments
end
post controller
def show
#current_user ||= User.find(session[:user_id]) if session[:user_id]
#commenter = #current_user
#post = Post.find(params[:id])
#comment = Comment.build_from( #post, #commenter.id, "234234" )
#comments = Comment.all
respond_to do |format|
format.html
format.json { render json: #post }
end
end
comments controller
class CommentsController < ApplicationController
def index
#comments = #post.comments.all
end
def create
#post = Post.find(params[:post_id])
#comment = #post.comments.new params[:comment]
if #comment.save
redirect_to #post # comment not save, so i dont redirect to this page
else
# is that there
end
end
end
post model
acts_as_commentable
has_many :comments
comment model
class Comment < ActiveRecord::Base
acts_as_nested_set :scope => [:commentable_id, :commentable_type]
attr_accessible :commentable, :body, :user_id
validates :body, :presence => true
validates :user, :presence => true
# NOTE: install the acts_as_votable plugin if you
# want user to vote on the quality of comments.
#acts_as_votable
belongs_to :commentable, :polymorphic => true
# NOTE: Comments belong to a user
belongs_to :user
# Helper class method that allows you to build a comment
# by passing a commentable object, a user_id, and comment text
# example in readme
def self.build_from(obj, user_id, comment)
new \
:commentable => obj,
:body => comment,
:user_id => user_id
end
#helper method to check if a comment has children
def has_children?
self.children.any?
end
# Helper class method to lookup all comments assigned
# to all commentable types for a given user.
scope :find_comments_by_user, lambda { |user|
where(:user_id => user.id).order('created_at DESC')
}
# Helper class method to look up all comments for
# commentable class name and commentable id.
scope :find_comments_for_commentable, lambda { |commentable_str, commentable_id|
where(:commentable_type => commentable_str.to_s, :commentable_id => commentable_id).order('created_at DESC')
}
# Helper class method to look up a commentable object
# given the commentable class name and id
def self.find_commentable(commentable_str, commentable_id)
commentable_str.constantize.find(commentable_id)
end
end
post view
%h2 Add a comment:
- #comments.each do |c|
= #c.body
= form_for([#post, #comment]) do |f|
.field
= f.label :body
%br/
= f.text_area :body
.actions
= f.submit
Thanks in advance and sorry for bad english
First of all you can debug why #comment.save return false yourself - just add p #comment.errors in else block and check server log.
It seems for me that you try to save invalid comments because you don't have setup user for #comment in action CommentsController#create. Comment validates presence of user!
There are several ways how to fix it. Analyzing your code I think the simplest way for you is modify CommentsController#create
#CommentsController
def create
#current_user ||= User.find(session[:user_id]) if session[:user_id]
#post = Post.find(params[:post_id])
#comment = #post.comments.new params[:comment]
#comment.user = #current_user
if #comment.save
redirect_to #post # comment not save, so i dont redirect to this page
else
# is that there
end
end
Another way is to use some gem for authentication - I recommend devise
One more way (very bad way) is to pass user_id through hidden field (you have defined #current_user in PostsController#show and user_id in attr_accessible list in Comment). But this is easy way to hack your application and write comments on behalf of any user in system!
I have a form that lets me create new blog posts and I'd like to be able to create new categories from the same form.
I have a habtm relationship between posts and categories, which is why I'm having trouble with this.
I have the following 2 models:
class Post < ActiveRecord::Base
has_and_belongs_to_many :categories
attr_accessible :title, :body, :category_ids
accepts_nested_attributes_for :categories # should this be singular?
end
class Category < ActiveRecord::Base
has_and_belongs_to_many :posts
attr_accessible :name
end
My form lets me pick from a bunch of existing categories or create a brand new one. My form is as follows.
# using simple_form gem
.inputs
= f.input :title
= f.input :body
# the line below lets me choose from existing categories
= f.association :categories, :label => 'Filed Under'
# I was hoping that the code below would let me create new categories
= f.fields_for :category do |builder|
= builder.label :content, "Name"
= builder.text_field :content
When I submit my form, it gets processed but the new category is not created. My command prompt output tells me:
WARNING: Can't mass-assign protected attributes: category
But, if I add attr_accessible :category, I get a big fat crash with error message "unknown attribute: category".
If I change the fields_for target to :categories (instead of category) then my form doesn't even display.
I've spent a while trying to figure this out, and watched the recent railscasts on nested_models and simple_form but couldn't get my problem fixed.
Would this be easier if I was using a has_many :through relationship (with a join model) instead of a habtm?
Thanks to everyone who answered. After much trial and error, I managed to come up with a fix.
First of all, I switched from a HABTM to a has_many :through relationship, calling my join model categorization.rb (instead of categorizations_posts.rb) - NB: the fix detailed below will likely work with a HABTM too:
Step 1: I changed my models to look like this:
# post.rb
class Post < ActiveRecord::Base
has_many :categorizations
has_many :categories, :through => :categorizations
attr_accessible :title, :body, :category_ids
accepts_nested_attributes_for :categories
end
#category.rb
class Category < ActiveRecord::Base
has_many :categorizations
has_many :posts, :through => :categorizations
attr_accessible :name, :post_ids
end
#categorization.rb
class Categorization < ActiveRecord::Base
belongs_to :post
belongs_to :category
end
From the post model above: obviously, the accessor named :category_ids must be present if you want to enable selecting multiple existing categories, but you do not need an accessor method for creating new categories... I didn't know that.
Step 2: I changed my view to look like this:
-# just showing the relevent parts
= fields_for :category do |builder|
= builder.label :name, "Name"
= builder.text_field :name
From the view code above, it's important to note the use of fields_for :category as opposed to the somewhat unintuitive fields_for :categories_attributes
Step 3
Finally, I added some code to my controller:
# POST /posts
# POST /posts.xml
def create
#post = Post.new(params[:post])
#category = #post.categories.build(params[:category]) unless params[:category][:name].blank?
# stuff removed
end
def update
#post = Post.find(params[:id])
#category = #post.categories.build(params[:category]) unless params[:category][:name].blank?
# stuff removed
end
Now, when I create a new post, I can simultaneously choose multiple existing categories from the select menu and create a brand new category at the same time - it's not a case of one-or-the-other
There is one tiny bug which only occurs when editing and updating existing posts; in this case it won't let me simultaneously create a new category and select multiple existing categories - if I try to do both at the same time, then only the existing categories are associated with the post, and the brand-new one is rejected (with no error message). But I can get round this by editing the post twice, once to create the new category (which automagically associates it with the post) and then a second time to select some additional existing categories from the menu - like I said this is not a big deal because it all works really well otherwise and my users can adapt to these limits
Anyway, I hope this helps someone.
Amen.
In your form you probably should render the fields_for once per category (you can have multiple categories per post, hence the habtm relation). Try something like:
- for category in #post.categories
= fields_for "post[categories_attributes][#{category.new_record? ? category.object_id : category.id}]", category do |builder|
= builder.hidden_field :id unless category.new_record?
= builder.label :content, "Name"
= builder.text_field :content
I have made my application and my nested form works with HABTM.
My model is :
class UserProfile < ActiveRecord::Base
attr_accessible :name, :profession
has_and_belongs_to_many :cities
belongs_to :user
attr_accessible :city_ids, :cities
def self.check_city(user,city)
user.cities.find_by_id(city.id).present?
end
end
class City < ActiveRecord::Base
attr_accessible :city_name
has_and_belongs_to_many :user_profiles
end
In my form I have:
-# just showing the relevent parts
= f.fields_for :cities do|city|
= city.text_field :city_name
And at my controller:
def create
params[:user_profile][:city_ids] ||= []
if params[:user_profile][:cities][:city_name].present?
#city= City.create(:city_name=>params[:user_profile][:cities][:city_name])
#city.save
params[:user_profile][:city_ids] << #city.id
end
#user=current_user
params[:user_profile].delete(:cities)
#user_profile = #user.build_user_profile(params[:user_profile])
respond_to do |format|
if #user_profile.save
format.html { redirect_to #user_profile, notice: 'User profile was successfully created.' }
format.json { render json: #user_profile, status: :created, location: #user_profile }
else
format.html { render action: "new" }
format.json { render json: #user_profile.errors, status: :unprocessable_entity }
end
end
end
def update
params[:user_profile][:city_ids] ||= []
if params[:user_profile][:cities][:city_name].present?
#city= City.create(:city_name=>params[:user_profile][:cities][:city_name])
#city.save
params[:user_profile][:city_ids] << #city.id
end
#user=current_user
params[:user_profile].delete(:cities)
#user_profile = #user.user_profile
respond_to do |format|
if #user_profile.update_attributes(params[:user_profile])
format.html { redirect_to #user_profile, notice: 'User profile was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: #user_profile.errors, status: :unprocessable_entity }
end
end
end
This code works.
Maybe you should try it with (not testet):
attr_accessible :category_attributes
And HBTM relations arent really recommened... But I use them on my own :P