The following code creates a select box
<%= select_tag "microposts", options_from_collection_for_select(#microposts, "id", "name"), { :prompt => 'All microposts' } %>
I want to show all the microposts in a select box, but i get the following error
NoMethodError in Managments#edit
undefined method `map' for nil:NilClass
Did you mean? tap
Can someone explain to me how to display all the microposts in a select box?
controller
class ManagmentsController < ApplicationController
before_action :logged_in_user, only: [:create, :destroy]
before_action :correct_user, only: :destroy
def index
#managments = current_user.managments
#micropost = current_user.microposts.build
end
def show
#microposts = Micropost.paginate(page: params[:page])
#managment = Managment.find_by(id: params[:id])
if !#managment
raise ActionController::RoutingError.new('Not Found')
end
#user = #managment.user
end
def new
#user = User.new
#managment = Managment.new
end
def edit
#managment = Managment.find(params[:id])
end
def create
#managment = current_user.managments.build(managment_params)
if #managment.save
flash[:success] = "Managment created!"
redirect_to #managment
else
#feed_items = current_user.feed.paginate(page: params[:page])
render 'new'
end
end
def update
#managment = Managment.find(params[:id])
if #managment.update(managment_params)
redirect_to #managment
else
render 'edit'
end
end
def destroy
#managment.destroy
flash[:success] = "Managment deleted"
redirect_to managments_path
end
private
def managment_params
params.require(:managment).permit(
:title, :budget,
:procent1, :procent2, :procent3, :procent4,
:procent5, :procent6, :procent7,
:procent8, :procent9, :procent10,
:procent11, :procent12, :result1,
:result2, :result3, :objectivesname1,
:objectivesname2, :objectivesname3,
:lowprocent1, :lowprocent2, :lowprocent3,
:medprocent1, :medprocent2, :medprocent3,
:highprocent1, :highprocent2, :highprocent3,
:lowobjectives1, :lowobjectives2, :lowobjectives3,
:medobjectives1, :medobjectives2, :medobjectives3,
:highobjectives1, :highobjectives2, :highobjectives3
)
end
def correct_user
#managment = current_user.managments.find_by(id: params[:id])
redirect_to managments_path if #managment.nil?
end
end
The Answer to the question can be find in the comments
def edit
#managment = Managment.find(params[:id])
#microposts = Micropost.paginate(page: params[:page])
end
Related
I want to give an id of "paper model" to "schedules model" and create a note_id.
However an error message "no implicit conversion of Symbol into Integer" shows.
Can someone help me to solve this problem?
papers/index.html.erb
<%= link_to "Go to Schedule", new_schedule_path(id: paper.id) %>
routes.rb
get '/schedules/new/:id', to: 'schedules#new', as: :schedules_new
schedules_controller
class ActionsController < ApplicationController
before_action :authenticate_user!
before_action :set_schedule, only: [:edit, :update, :destroy]
def new
#schedule = Schedule.new
end
def create
#schedule = Schedule.new(schedule_params)
#schedule.user_id = current_user.id
#schedule.note_id = params[:id]
if #schedule.save
redirect_to schedules_path, notice: "A schedule was saved!"
end
end
def index
#schedules = Schedule.all
end
def update
end
def delete
end
Private
def schedule_params
params.require(:schedule).permit(:note_id, :user_id, :params[:id])
end
def set_schedule
#schedule = Schedule.find(params[:id])
end
end
params
=> "31", "controller"=>"schedules", "action"=>"new"} permitted: false>
You are not even using shedule_params values, as you override them afterwards ......
Then you could create empty Schedule object and then assign values ...
def create
#schedule = Schedule.new
#schedule.user_id = current_user.id
#schedule.note_id = params[:id]
if #schedule.save
redirect_to schedules_path, notice: "A schedule was saved!"
end
end
Or if I am correct with your relations, it could be also
def create
#schedule = current_user.schedules.new
#schedule.note_id = params[:id]
if #schedule.save
redirect_to schedules_path, notice: "A schedule was saved!"
end
end
I could solve this problem as follow.
Controller:
def new
#schedule = Schedule.new
#schedule.note_id = params[:id]
end
def create
#schedule = Schedule.new(schedule_params)
#schedule.user_id = current_user.id
if #schedule.save
redirect_to schedules_path, notice: "A schedule was saved!"
else
render 'new'
end
end
.
.
.
Private
def schedule_params
params.require(:schedule).permit(:note_id, :user_id)
end
Add into View
<%= f.hidden_field :id, :value => #schedule.note_id %>
I'm getting this problem:
uninitialized constant QuestionsController::Question
def index
#Question
#preguntas = Question.all
#project_id = request.original_url.split('.').last
set_current_project(#project_id)
if(#project_id.include? "http")
On my QuestionController randomly after changing nothing from my application, any idea what may be causing it? here's the complete .rb file:
class QuestionsController < ApplicationController
before_action :require_user
before_action :require_project
before_action :require_user, except: [:new, :create]
before_action :current_project, only: [:index]
def index
#preguntas = Question.all
#project_id = request.original_url.split('.').last
set_current_project(#project_id)
if(#project_id.include? "http")
#project_id = "0"
end
if(#project_id != "0")
#proyecto = Project.find(#project_id)
end
end
def show
#pregunta = Question.find(params[:id])
end
def new
#pregunta = Question.new
end
def create
#pregunta = Question.new(pregunta_params)
if #pregunta.save
redirect_to #pregunta
else
render 'new'
end
end
def edit
#pregunta = Question.find(params[:id])
end
def update
#pregunta = Question.find(params[:id])
if #pregunta.update(pregunta_params)
redirect_to #pregunta
else
render 'edit'
end
end
def destroy
#pregunta = Question.find(params[:id])
#pregunta.destroy
flash[:danger] = "Se ha borrado la pregunta"
redirect_to questions_path
end
def require_same_user
set_project
if current_user != #project.user && !#current_user.admin?
flash[:danger] = "Solo puedes editar tus artÃculos"
redirect_to root_path
end
end
def require_project
if current_user.projects.count <1 && !current_user.admin?
redirect_to root_path
end
end
private
def pregunta_params
params.require(:question).permit(:question, :value, :phase, :area, :input)
end
end
Ensure that the Question model is defined in a file named app/models/question.rb like this:
class Question < ApplicationRecord
# methods...
end
In the goals show page:
<%= link_to goals_path, class: "btn" do %>
<span class="glyphicon glyphicon-list"></span> Goals
<% end %>
but instead of being taken to my own goals_path I want to be taken to the goals_path of whichever user's goal show page I'm looking at.
goals_controller
class GoalsController < ApplicationController
before_action :set_goal, only: [:show, :edit, :update, :destroy, :like]
before_action :logged_in_user, only: [:create, :destroy]
before_action :correct_user, only: [:edit, :update, :destroy]
def index
if params[:tag]
#goals = Goal.tagged_with(params[:tag])
else
#accomplished_goals = current_user.goals.accomplished.order("deadline")
#unaccomplished_goals = current_user.goals.unaccomplished.order("deadline")
end
end
def show
#goal = Goal.find(params[:id])
#commentable = #goal
#comments = #commentable.comments
#comment = Comment.new
#notable = #goal
#notes = #notable.notes
#note = Note.new
#correct_user = current_user.goals.find_by(id: params[:id])
end
def new
#goal = current_user.goals.build
end
def edit
end
def create
#goal = current_user.goals.build(goal_params)
if (params[:commit] == 'conceal')
#goal.conceal = true
#goal.save
redirect_to #goal, notice: 'Goal was successfully created'
elsif
#goal.save
track_activity #goal
redirect_to #goal, notice: 'Goal was successfully created'
else
flash.now[:danger] = 'Required Field: "Enter Goal"'
render 'new'
end
end
def update
if #goal.update(goal_params)
redirect_to goals_url, notice: 'Goal was successfully updated'
else
render action: 'edit'
end
end
def destroy
#goal.destroy
redirect_to goals_url
end
def like
#goal = Goal.find(params[:id])
#goal_like = current_user.goal_likes.build(goal: #goal)
if #goal_like.save
#goal.increment!(:likes)
flash[:success] = 'Thanks for liking!'
else
flash[:error] = 'Two many likes'
end
redirect_to(:back)
end
private
def set_goal
#goal = Goal.find(params[:id])
end
def correct_user
#goal = current_user.goals.find_by(id: params[:id])
redirect_to root_url, notice: "Not authorized to edit this goal" if #goal.nil?
end
def goal_params
params.require(:goal).permit(:name, :like, :deadline, :accomplished, :tag_list, :comment, :private_submit)
end
end
Please let me know if you need further explanation or code to help you help me :-]
If you have a target user defined (or have a way to access a user's id), you can do something like:
<% link_to goals_path(user_id: #goal.user_id) do %>
<span class="glyphicon glyphicon-list"></span> Goals
<% end %>
Then you can modify your index method to handle a user_id param:
def index
if params[:tag]
#goals = Goal.tagged_with(params[:tag])
elsif params[:user_id]
#goals = User.find(params[:user_id]).goals
else
#accomplished_goals = current_user.goals.accomplished.order("deadline")
#unaccomplished_goals = current_user.goals.unaccomplished.order("deadline")
end
end
This should render a particular user's goals if their id is passed in as a param. Hope this helps!
You could also just take a completely different route, and be a little more REST-ful about grabbing users/:user_id/goals, and add it as a route in your routes.rb
get "/users/:user_id/goals", to: "goals#user_goals", as: "user_goals"
and update your button to have
<% link_to user_goals_path(#goal_user) ... %>
and then add the goals#user_goals method
def user_goals
#goals = Goal.find_by({user_id: params[:user_id]})
render :index # or some other view
end
I created a feed following http://railscasts.com/episodes/406-public-activity?view=asciicast.
In that feed I'd like it to say,
"User Name" added/updated value "Value Name" with the comment "Comment Content".
This partial creates the "Value Name" part:
views/public_activity/comment/_create.html.erb
added value
<% if activity.trackable %>
<b><%= link_to activity.trackable.name, activity.trackable %></b>
<% else %>
which has since been removed
<% end %>
This partial creates the "Comment Content" part:
views/public_activity/valuation/_create.html.erb
with the comment
<% if activity.trackable %>
<%= link_to activity.trackable.content %>
<% else %>
which has since been removed
<% end %>
If I add name or content to either one of the partials I am given an undefined method error message. What code would I need to add to their controllers or application controller to make something like this work?
Ideally, with the comment "Comment Content". would only be generated in instances where a comment is created so that, with the comment, wouldn't be left hanging.
class CommentsController < ApplicationController
before_action :load_commentable
before_action :set_comment, only: [:show, :edit, :update, :destroy]
before_action :logged_in_user, only: [:create, :destroy]
def index
#comments = #commentable.comments
end
def new
#comment = #commentable.comments.new
end
def create
#comment = #commentable.comments.new(comment_params)
if #comment.save
#comment.create_activity :create, owner: current_user
redirect_to #commentable, notice: "comment created."
else
render :new
end
end
def edit
#comment = current_user.comments.find(params[:id])
end
def update
#comment = current_user.comments.find(params[:id])
if #comment.update_attributes(comment_params)
redirect_to #commentable, notice: "Comment was updated."
else
render :edit
end
end
def destroy
#comment = current_user.comments.find(params[:id])
#comment.destroy
#comment.create_activity :destroy, owner: current_user
redirect_to #commentable, notice: "comment destroyed."
end
private
def set_comment
#comment = Comment.find(params[:id])
end
def load_commentable
resource, id = request.path.split('/')[1, 2]
#commentable = resource.singularize.classify.constantize.find(id)
end
def comment_params
params.require(:comment).permit(:content, :commentable)
end
end
class ValuationsController < ApplicationController
before_action :set_valuation, only: [:show, :edit, :update, :destroy]
before_action :logged_in_user, only: [:create, :destroy]
def index
if params[:tag]
#valuations = Valuation.tagged_with(params[:tag])
else
#valuations = Valuation.order('RANDOM()')
end
end
def show
#valuation = Valuation.find(params[:id])
#commentable = #valuation
#comments = #commentable.comments
#comment = Comment.new
end
def new
#valuation = current_user.valuations.build
end
def edit
end
def create
#valuation = current_user.valuations.build(valuation_params)
if #valuation.save
redirect_to #valuation, notice: 'Value was successfully created'
else
#feed_items = []
render 'pages/home'
end
end
def update
if #valuation.update(valuation_params)
redirect_to #valuation, notice: 'Value was successfully updated'
else
render action: 'edit'
end
end
def destroy
#valuation.destroy
redirect_to valuations_url
end
private
def set_valuation
#valuation = Valuation.find(params[:id])
end
def correct_user
#valuation = current_user.valuations.find_by(id: params[:id])
redirect_to valuations_path, notice: "Not authorized to edit this valuation" if #valuation.nil?
end
def valuation_params
params.require(:valuation).permit(:name, :private_submit, :tag_list, :content, :commentable, :comment)
end
end
class ApplicationController < ActionController::Base
include PublicActivity::StoreController
#before_action :load_todays_habits
before_action :set_top_3_goals
before_action :randomize_value
before_action :set_stats
protect_from_forgery with: :exception
include SessionsHelper
def set_top_3_goals
#top_3_goals = current_user.goals.unaccomplished.top_3 if current_user
end
def randomize_value
#sidebarvaluations = current_user.valuations.randomize if current_user
end
def set_stats
#quantifieds = Quantified.joins(:results).all
#averaged_quantifieds = current_user.quantifieds.averaged if current_user
#instance_quantifieds = current_user.quantifieds.instance if current_user
end
hide_action :current_user
private
#def load_todays_habits
# #user_tags = current_user.habits.committed_for_today.tag_counts if current_user
# #all_tags = Habit.committed_for_today.tag_counts if current_user
#end
# Confirms a logged-in user.
def logged_in_user
unless logged_in?
store_location
flash[:danger] = "Please log in."
redirect_to login_url
end
end
end
Thank you so much for your time.
So that multiple people can be an administrator to a business page, we've created a model called administration where people can apply to be an admin of a business and thus the status of "0" is "pending" and "1" is accepted.
How can I prevent users from editing a page where their status for i is still "0" (pending).
class Administration < ActiveRecord::Base
attr_accessible :business_id, :user_id, :status
belongs_to :user
belongs_to :business
scope :pending, where('status = ?',0).order("updated_at desc")
def self.new_by_user_business( user, business)
admin = self.new
admin.business_id = business.id
admin.user_id = user.id
admin.status = 0
admin.save!
end
end
Here is the current "edit page"
<h1>Editing business</h1>
<%= render 'form1' %>
Here is the business controller.
class BusinessesController < ApplicationController
respond_to :html, :xml, :json
before_filter :authenticate_user!, except: [:index, :show]
def index
#businesses = Business.all
respond_with(#businesses)
end
def show
#business = Business.find(params[:id])
if request.path != business_path(#business)
redirect_to #business, status: :moved_permanently
end
end
def new
#business = Business.new
3.times { #business.assets.build }
respond_with(#business)
end
def edit
#business = get_business(params[:id])
#avatar = #business.assets.count
#avatar = 3-#avatar
#avatar.times {#business.assets.build}
end
def create
#business = Business.new(params[:business])
if #business.save
redirect_to #business, notice: 'Business was successfully created.'
else
3.times { #business.assets.build }
render 'new'
end
end
def update
#business = get_business(params[:id])
if #business.update_attributes(params[:business])
flash[:notice] = "Successfully updated Business."
end
#avatar = #business.assets.count
#avatar = 3-#avatar
#avatar.times {#business.assets.build}
respond_with(#business)
end
def destroy
#business = get_business(params[:id])
#business.destroy
respond_with(#business)
end
def my_business
#business = Business.all
end
def business_tickets
#user = current_user
#business = get_business(params[:id])
#tickets = #business.tickets
#business_inbox = TicketReply.where(:email => #business.callred_email)
end
def your_business
#user = current_user
#business = get_business(params[:id])
if #business.users.map(&:id).include? current_user.id
redirect_to my_business_businesses_path, notice: 'You are already an administator of this business.'
else
#admin = Administration.new_by_user_business( #user, #business)
BusinessMailer.delay(queue: "is_your_business", priority: 20, run_at: 5.minutes.from_now).is_your_business(#user,#business)
redirect_to #business, notice: 'Thank you for claiming your business, and we will be in touch with you shortly.'
end
end
def view_message
# #business = Business.find(params[:business_id])
#ticket = Ticket.find(params[:id])
#reply = #ticket.ticket_replies
end
private
def get_business(business_id)
#business = Business.find(business_id)
end
end
You could add a before_filter to check the status. You will have to change some of the logic but this is the idea
class BusinessesController < ApplicationController
before_filter :restrict_access, :only => [:edit, :update]
private
def restrict_access
#business = get_business(params[:id])
redirect to root_path, :notice => "Not Authorized" unless current_user.status == 1
end
end