I'm trying to create "group posts" that are connected to a "user group" and a user. The first line of my create method is failing with this error:
Couldn't find UserGroup with 'id'=
I've been looking at blog building tutorials thinking that my "group post" is acting like a comment but instead of being attached to an article it's attached to a "user group".
I'm pretty new to Rails so it could be simply a syntax issue. Any advise would be appreciated.
Here is the group post create method:
def create
#user_group = UserGroup.find(params[:user_group_id])
#group_post = current_user.group_posts.new(group_post_params)
if #group_post.save
respond_to do |format|
format.html {redirect_to user_group_path(#user_group), notice: "Group Post created!"}
end
else
redirect_to user_group_path(#user_group), notice: "Something went wrong."
end
end
private
def group_post_params
params.require(:group_post).permit(:content, :post_type, :user_group_id)
end
Here is the user group model:
class UserGroup < ActiveRecord::Base
has_many :group_members, dependent: :destroy
has_many :members, through: :group_members, source: :user
has_many :group_posts, dependent: :destroy
validates :name, presence: true, length: {minimum: 5}
validates :searchable, presence: true, length: {minimum: 5}
def owners
members.includes(:group_members).where('group_members.owner = ?', true)
end
def regular_users
members.includes(:group_members).where('group_members.owner = ?', false)
end
end
Here is the group post model:
class GroupPost < ActiveRecord::Base
include PublicActivity::Model
belongs_to :user
belongs_to :user_group
validates :user_id, :presence => true
validates :content, :presence => true
end
And finally the routes:
Rails.application.routes.draw do
devise_for :users, controllers: {registrations: 'registrations'}
root 'pages#home'
resources :users, only: [:index, :show]
resources :friendships, only: [:create, :destroy, :accept] do
member do
put :accept
end
end
resources :posts, only: [:create, :edit, :update, :destroy]
resources :group_posts, only: [:create, :edit, :update, :destroy]
resources :activities, only: [:index] do
member do
put "upvote" =>"activities#upvote"
end
end
resources :user_groups do
resources :group_posts
resources :group_members
end
end
Here is the form partial submitting the group post:
<div class="posts-panel">
<%= form_for(#group_post) do |f| %>
<%= render 'partials/error_messages', object: f.object %>
<div class="form-group">
<%= f.text_area :content, placeholder: "Compose new post...", class: "form-control" %>
</div>
<div class="form-group">
<%= f.select :post_type, [['Request', 'request'],['Report', 'report']], {}, {class: "form-control"} %>
</div>
<div class="form-group">
<%= hidden_field_tag :user_group_id, #usergroup.id %>
<%= f.submit "Post", class: "btn btn-primary" %>
</div>
<% end %>
</div>
Complete Params:
{"utf8"=>"✓", "authenticity_token"=>"thkRQYNcl+ySSoWIE83V22DEqYdttg+TF4coFsmasXkt2mylgB2YG/vAl2KYRey/djTqL5iNSTIyWJpsSWyCQQ==", "group_post"=>{"content"=>"This is it", "post_type"=>"prayer"}, "user_group_id"=>"1", "commit"=>"Post", "controller"=>"group_posts", "action"=>"create"}
The user_group_id was not inside the params hash. Adding the following line to the create method solved the issue:
#group_post.user_group = #user_group
Related
I am having an issue with my update method in my recipes controller where I cannot create a new category. Category is a nested resource within recipes. This is my update method
def update
if current_user.id != #recipe.category.user_id
flash.now[:notice] = "You cannot update a recipe that you did not author."
redirect_to recipes_path
else
#recipe.update(recipe_params)
flash.now[:notice] = "#{#recipe.title} has been updated."
redirect_to recipe_path
end
if #category
recipe.build_category
end
end
I just recently added the if #category recipe.build_category but that didn’t do anything.
This is my recipe strong params:
def recipe_params
params.require(:recipe).permit(
:title,
:description,
:category_id,
category_attributes: [:name],
instructions_attributes: [:id,
:step, :_destroy
]
)
end
And my recipes controller before actions:
before_action :redirect_if_not_logged_in
before_action :find_recipe, only: [:show, :edit, :update, :destroy]
before_action :find_category, only: [:index, :new, :create]
I also have this language in my recipes _form:
<%= f.fields_for :category, recipe.build_category do |cb| %>
<div style="margin-left:30px">
<%= cb.label :name %>
<%= cb.text_field :name %>
</div>
<% end %>
and then this in my recipes edit.html.erb
<%= render partial: "form", locals: {recipe: #recipe, category: #category, button_name: "Update Recipe"}%>
These are my recipes & categories routes:
resources :recipes, only: [:index, :show, :new]
end
resources :recipes do
resources :categories, only: [:index, :new, :create]
end
resources :users, only: [:show, :new] do
resources :recipes, only: [:index]
end
This is my recipes model:
belongs_to :category
accepts_nested_attributes_for :category, reject_if: :all_blank
has_many :instructions, dependent: :destroy
accepts_nested_attributes_for :instructions, reject_if: :all_blank, allow_destroy: true
validates :title, presence: true
validates :instructions, presence: true
validates :description, presence: true
end
Does anyone know why I wouldn’t be able to create the category within an existing recipe?
**Side note - my flash errors are also not working. Thanks in advance for any help.
I have Producers nested inside of Islands, however my Producer#Index shows all the Islands. I expect to have each Producer index view to only list Producers for the parent Island. Please help!
routes.rb
Rails.application.routes.draw do
devise_for :users
root to: 'islands#index'
resources :islands do
resources :producers
end
end
islands.rb
class Island < ApplicationRecord
has_many :producers, dependent: :destroy
accepts_nested_attributes_for :producers
validates :island_name, uniqueness: true, presence: true
validates :island_country, presence: true
validates_associated :producers
end
producers.rb
class Producer < ApplicationRecord
belongs_to :user
belongs_to :island
end
producers_controller.rb
class ProducersController < ApplicationController
skip_before_action :authenticate_user!, only: [ :index ]
before_action :set_island
before_action :set_producer, only: [ :show, :edit, :update, :destroy]
def index
#producers = Producer.where(island_id: params[:island_id])
#producers = policy_scope(Producer)
end
...
private
def set_island
#island = Island.find(params[:island_id])
end
def set_producer
#producer = #island.producers.find(params[:id])
authorize #producer
end
def producer_params
params.require(:producer).permit(:producer_name, :email, :address1, :address2, :postal_code, :city, :country, :island_id)
end
end
producers/index.html.erb
<div class="container">
<h1>Producers from <%= #island.island_name %></h1>
<ul>
<% #producers.each do |producer| %>
<div class="container">
<h5><%= producer.producer_name %></h5>
<h5><%= link_to "details", island_producer_path(producer.island, producer)%></h5>
</div>
<% end %>
</ul>
</div>
You need to pass the scope into policy_scope:
class Island < ApplicationRecord
has_many :producers
end
class ProducersController < ApplicationController
def index
#producers = policy_scope(#island.producers)
end
end
I'm getting the error from line #7 of my views when I'm trying to create a new room:
undefined method `rooms_path' for #<#<Class:0x007fb46b5db2a0>:0x007fb46b5d9ec8>
I'm a little confused about why am I getting this error. I have built a relationship between Room and Facility that each Facility has many Rooms. The Facility part works bug free, I've been able to create/edit/show facilities.
Views (views/rooms/new.html.erb)
<div class="panel panel-default">
<div class="panel-heading">
Create your beautiful room
</div>
<div class="panel-body">
<div class="container">
<%= form_for #room do |f| %>
<div class="form-group">
<label>Name</label>
<%=f.text_field :room_name, placeholder: "What is the name of the room?", class: "form-control", required: true%>
</div>
<div><%= f.submit "Create This Room", class: "btn btn-normal" %></div>
<%end%>
</div>
</div>
</div>
My Room model (models/room.rb):
class Room < ApplicationRecord
belongs_to :facility
validates :room_type, presence: true
validates :room_name, presence: true
end
My Facility model (models/facility.rb):
class Facility < ApplicationRecord
belongs_to :user
has_many :photos
has_many :rooms
validates :facility_name, presence: true
validates :address, presence: true
validates :license, presence: true
validates :phone, presence: true
def cover_photo(size)
if self.photos.length > 0
self.photos[0].image.url(size)
else
"blank.jpg"
end
end
end
My Rooms Controller(controllers/rooms_controller.rb)
class RoomsController < ApplicationController
before_action :set_room, except: [:index, :new, :create]
def index
#facility = Facility.find(params[:facility_id])
#rooms = #facility.rooms
end
def new
#facility = Facility.find(params[:facility_id])
#room = #facility.rooms.build
end
end
Routes (routes.rb)
Rails.application.routes.draw do
devise_for :users,
path: '',
path_names: {sign_in: 'login', sign_out: 'logout', edit: 'profile', sign_up: 'registration'},
controllers: {omniauth_callbacks: 'omniauth_callbacks', registrations: 'registrations'}
resources :users, only: [:show]
resources :facilities, except: [:edit] do
member do
get 'listing'
get 'pricing'
get 'features'
get 'services'
get 'types_care'
get 'photo_upload'
get 'room_upload'
end
resources :rooms, except: [:edit] do
member do
get 'listing'
get 'pricing'
get 'photo_upload'
end
end
resources :photos, only: [:create, :destroy]
end
end
Been stuck on this for a couple of days and would really appreciate the help :) Thanks so much in advance!
form_for generate url based on class of #room, it doesn't include outer resource. Thus, you have to tell Rails to do that explicitly using below statement:
<%= form_for [#room.facility, #room] do |f| %>
Choose any of the following for form_for instead of <%= form_for #room do |f| %> as your routes are nested
<%= form_for #room, url: [#room.facility, #room] do |f| %>
<%= form_for #room, url: [:facility, #room] do |f| %>
<%= form_for [#room.facility, #room] do |f| %>
<%= form_for [:facility, #room] do |f| %>
Here you can get more detail about form_for helper
I hope I am not asking an obvious question/ wont get down voted to hell for this.I am working on rails app and I am getting a "param is missing or the value is empty" error.
I have an event and questions that have already been created and I am using a nested form to allow the user to answer all the questions at once.
I am using rails 4
Models
class Event < ActiveRecord::Base
belongs_to :user
has_many :questions
accepts_nested_attributes_for :questions, allow_destroy: true
end
class Question < ActiveRecord::Base
belongs_to :user
belongs_to :event
has_many :answers
accepts_nested_attributes_for :answers, allow_destroy: true
end
class Answer < ActiveRecord::Base
belongs_to :user
belongs_to :question
end
Routes.rb
Rails.application.routes.draw do
root 'home#index'
devise_for :users, path_names: {sign_in: "login", sign_out: "logout"}, controllers: {omniauth_callbacks: "omniauth_callbacks"}
resources :answers
resources :users, only: [:new, :create]
resources :questions do
resources :answers #-> domain.com/questions/1/answers/new
end
resources :events, only: [:index, :new, :show, :update] do
patch ":id", action: :index
collection do
get :favorite
get "question/:id", action: :question
end
end
get 'users/new', to: 'users#new'
post 'users/new', to: 'users#create'
get 'events/favorite', to: 'events#favorite', via:[:get], as: 'favorite'
post 'events/:id' => 'events#update'
get 'answers/new' => 'answers#new'
get 'events/question' => 'events#question'
end
answers controller
class AnswersController < ApplicationController
def new
#event = Event.find(params[:id])
#answer = Answer.new
end
def show
end
def create
#answer = Answer.new(answer_params)
#answer.save
redirect_to events_path, notice: "Answered Questions"
end
private
def answer_params
params.require(:answer).permit(:response, :question, :event, :user, :event_id, :question_id, :user_id)
end
end
This is where my issue lies. Originally I had a very generic nested from a la http://railscasts.com/episodes/196-nested-model-form-revised but I switched the form_for down to the #answer because that is whats being created and switched to a button_to because the submit button was not writing the answer to the DB.(I believe it was trying to trigger something with #event )
<h1>New answers</h1>
<%= fields_for #event do |f| %>
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<%= f.fields_for :questions do |b| %>
<p>
<%= b.text_field :poll%><br />
<%= form_for #answer do |x| %>
<%= x.text_field :response %>
<% end %>
</p>
<% end %>
<%= button_to "New", action: "create"%>
<% end %>
<%= link_to 'Back', answers_path %>
Please let me know if you need anymore code or have any questions
Thanks!
UPDATE
I have reworked my code based off this blog post http://iroller.ru/blog/2013/10/14/nested-model-form-in-rails-4/
now I am running the update through the events controller or at least I'm trying to.
The code is as follows, the error im getting now is
undefined local variable or method `event_params' for #
Thanks guys and girls sorry for the dumb questions
Models
class Event < ActiveRecord::Base
belongs_to :user
has_many :questions
accepts_nested_attributes_for :questions
end
class Question < ActiveRecord::Base
belongs_to :user
belongs_to :event
has_many :answers
accepts_nested_attributes_for :answers
end
class Answer < ActiveRecord::Base
belongs_to :user
belongs_to :question
end
Routes.rb
Rails.application.routes.draw do
root 'home#index'
devise_for :users, path_names: {sign_in: "login", sign_out: "logout"}, controllers: {omniauth_callbacks: "omniauth_callbacks"}
resources :answers
resources :users, only: [:new, :create]
resources :questions do
resources :answers #-> domain.com/questions/1/answers/new
end
resources :events, only: [:index, :new, :show, :update] do
patch ":id", action: :index
collection do
get :favorite
get "question/:id", action: :question
end
end
get 'users/new', to: 'users#new'
post 'users/new', to: 'users#create'
get 'events/favorite', to: 'events#favorite', via:[:get], as: 'favorite'
post 'events/:id' => 'events#update'
get 'answers/new' => 'answers#new'
get 'events/question' => 'events#question'
end
methods from events_controller
def question
#event = Event.find(params[:id])
end
def update
#event = Event.find(params[:id])
if #event.update(event_params)
redirect_to events_path, notice: "Answers saved"
else
redirect_to events_question_path, notice: "Answers not saved"
end
questions.erb
<%= simple_form_for(#event) do |f| %>
<%= f.error_notification %>
<%= f.object.name %>
<%= f.simple_fields_for :questions, f.object.questions do |q| %>
<%= q.object.poll%>
<%= q.simple_fields_for :answers, q.object.answers.build do |a|%>
<%= a.text_field :response %>
<% end %>
<%end %>
<%= f.button :submit%>
<% end %>
I recently added a new Model (discussion.rb) and Controller (discussions_controller.rb). I am trying to get postcomments to work with discussions.
discussion.rb
class Discussion < ActiveRecord::Base
belongs_to :user
has_many :postcomments, dependent: :destroy
validates :user_id, presence: true
validates :content, presence: true
attr_accessible :content, :user_id
default_scope order: 'discussions.created_at DESC'
end
Here's what I have in routes
resources :discussions, :path => "disc"
resources :users do
member do
get :following, :followers
end
end
resources :sessions, only: [:new, :create, :destroy]
resources :microposts, only: [:create, :destroy]
resources :discussions, only: [:create, :destroy]
resources :relationships, only: [:create, :destroy]
resources :microposts do
resources :postcomments
resources :discussions do
resources :postcomments
end
end
here's the postcomments model
class Postcomment < ActiveRecord::Base
attr_accessible :comment_content
belongs_to :user
belongs_to :micropost
belongs_to :discussion
validates :comment_content, presence: true
validates :user_id, presence: true
default_scope order: 'postcomments.created_at ASC'
end
I'm trying to use this in view except, I get the error posted in the title
<%= form_for([discussion, #comment]) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field">
<%= f.text_field :comment_content %>
</div>
<div class="ItemContainer">
<div class="ItemInput">
<button class="btn" type="submit">
Comment
</button>
</div><div class="ItemCommentCount">
<% end %>
It says that the error is caused from this line
<%= form_for([discussion, #comment]) do |f| %>
Anyone know how I can fix this path problem?
class PostcommentsController < ApplicationController
def create
#micropost = Micropost.find(params[:micropost_id])
#comment = Postcomment.new(params[:postcomment])
#comment.micropost = #micropost
#comment.user = current_user
if #comment.save
redirect_to(:back)
else
render partial: 'shared/_postcomment_form', locals: { micropost: #micropost }
end
end
def createdisc
#discussion = Discussion.find(params[:discussion_id])
#comment = Postcomment.new(params[:postcomment])
#comment.discussion = #discussion
#comment.user = current_user
if #comment.save
redirect_to(:back)
else
render partial: 'shared/_postcomment_form', locals: { discussion: #discussion}
end
end
end
Try consolidating your use of resources :discussions in your routes file. I've edited below assuming you didn't intend to nest discussions under microposts.
resources :discussions, only: [:create, :destroy], path: "disc" do
resources :postcomments
end
resources :users do
member do
get :following, :followers
end
end
resources :sessions, only: [:new, :create, :destroy]
resources :microposts, only: [:create, :destroy]
resources :relationships, only: [:create, :destroy]
resources :microposts do
resources :postcomments
end
This line
<%= form_for([discussion, #comment]) do |f| %>
This discussion should be an instance variable as well:
<%= form_for([#discussion, #comment]) do |f| %>
Then you need to define #discussion in the controller