For a university group project we are creating a rails application where Uni courses can be created, accessed etc.
One of the requirements for the project "A Course Coordinator must be able to set a user as a marker for a given course". To solve this, there is a Role Table which stores user_id, course_id, and the role, in this case "Marker".
I have created a view where the Course Coordinator can select a user from all Users and Set them as a Marker. This occurs on the courses/assign_marker page. The controller method for this page is shown below:
def assign_marker
@users = User.enrolled_users
@course = Course.find(params[:id])
end
Here, enrolled_users just returns all users which aren't admins (this works), and params[:id] is the course_id. If I use puts I can see that it has the correct value, so that's all working.
My problem occurs in my function to handle the post request:
def add_marker
Role.assign_marker(params[:user_id], params[:id])
# Flash
flash[:notice] = "#{User.find(params[:user_id]).username} was assigned as Marker to #{Course.find(params[:id]).courseName}"
# Refresh
redirect_back(fallback_location: root_path)
end
Upon submitting my form, I find that add_marker sees params[:id] as blank. Below is my form in haml (which corresponds to the assign_marker function)
= form_with url: 'assign_marker', :id => "#{@course.id}" do |f|
.row
.input-field
-# Display username in selection & store id as parameter
= f.collection_select(:user_id, @users, :id, :username, :include_blank => true)
%label Users
= button_tag "Set as Marker", {:class => "waves-effect waves-light btn"}
If I place #{@course.id} anywhere within the view I can see the correct course_id displayed. I attempted to set the id in the form_with as shown above, since that is what I'd do with form_tag. Clearly that isn't working, and add_marker isn't seeing it through params[:id].
Also, my routes for the above get and post requests are as follows:
get 'courses/assign_marker', :to => 'courses#assign_marker'
post 'courses/assign_marker', :to => 'courses#add_marker'
I'm fairly new to rails so I'm probably missing something pretty obvious. Any help would be much appreciated :). Thank you
The reason :id => "#{@course.id}"
is not working is because that's just used to set the HTML id property, you can see more about that here
Best solution is to use a nested route. Assuming you have resources :courses
somewhere on config/routes.rb
you should declare like this:
resources :courses do
member do
get :assign_marker
post :add_marker
end
end
That will generate the following routes:
GET /courses/:id/assign_marker
POST /courses/:id/add_marker
You can then reference their paths like this:
assign_market_course(@course)
add_market_course(@course)
The other solution, which may sound simpler but it's not really the "Rails way" is to add the id as a hidden field, something like this:
= f.hidden_field :id, value: @course.id
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.