I believe that in order to handle user interests and integrate them in user preferences, the required routes, controller actions, and templates must be created.
However, that’s just my viewpoint, and it’s simply designed. I have not yet begun construction on it. It might not go as planned if I attempt.
Add a Route for User Preferences:
- Add a route that will handle requests to /u/:username/preferences/interests.
```ruby
# plugins/custom_homepage/config/routes.rb
Discourse::Application.routes.append do
get '/u/:username/preferences/interests' => 'custom_homepage#interests', constraints: { username: /[^\/]+/ }
end
```
Create the Controller Action:
- Define a controller to handle the request and fetch the user’s interests.
```ruby
# plugins/custom_homepage/app/controllers/custom_homepage_controller.rb
class CustomHomepageController < ApplicationController
requires_login
def interests
username = params[:username]
user = User.find_by(username: username)
if user
render json: {
interests: user.custom_fields['interests']
}
else
render json: { error: 'User not found' }, status: 404
end
end
end
```
Add Interests Field in User Preferences:
- Modify the user preferences template to include an input for interests.
```html
<!-- plugins/custom_homepage/assets/javascripts/discourse/connectors/user-preferences/interests.hbs -->
<div class="control-group">
<label class="control-label">{{i18n 'user.preferences.interests'}}</label>
<div class="controls">
{{input value=model.custom_fields.interests class="form-control"}}
</div>
</div>
```
Extend the Serializer:
- Extend the user serializer to include the interests field.
```ruby
# plugins/custom_homepage/plugin.rb
after_initialize do
User.register_custom_field_type('interests', :string)
add_to_serializer(:current_user, :interests) do
object.custom_fields['interests']
end
add_to_serializer(:admin_detailed_user, :interests) do
object.custom_fields['interests']
end
end
```
Modify the JavaScript for Handling Interests:
- Use JavaScript to fetch and save the interests to the user profile.
```javascript
// plugins/custom_homepage/assets/javascripts/discourse/initializers/custom-homepage.js.es6
import { withPluginApi } from 'discourse/lib/plugin-api';
export default {
name: 'custom-homepage',
initialize(container) {
withPluginApi('0.8', api => {
api.modifyClass('controller:preferences/account', {
save() {
const interests = this.model.custom_fields.interests;
this.model.save().then(() => {
fetch(`/u/${this.model.username}/preferences/interests`, {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
'X-CSRF-Token': Discourse.CSRFToken
},
body: JSON.stringify({ interests })
}).then(response => response.json()).then(() => {
this.flash('Your preferences have been saved.');
});
});
}
});
});
}
};
```
-
Handle the PUT Request:
- Implement the logic to handle the PUT request and save the interests in the controller.
# plugins/custom_homepage/app/controllers/custom_homepage_controller.rb class CustomHomepageController < ApplicationController requires_login def interests if request.get? username = params[:username] user = User.find_by(username: username) if user render json: { interests: user.custom_fields['interests'] } else render json: { error: 'User not found' }, status: 404 end elsif request.put? user = current_user interests = params[:interests] user.custom_fields['interests'] = interests user.save_custom_fields render json: success_json end end end