Reputation: 63
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>
Upvotes: 0
Views: 110
Reputation: 63
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 %>
Upvotes: 0
Reputation: 6100
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
Upvotes: 3