Reputation: 6246
I am new to rails and am wondering if this is the best way to go about what I want to do.
I have a controller that creates entities.
Depending what gets submitted I want to either create a new entity from scratch, or copy an existing one.
So far I have
def create
if(params[:copy])
return copy_existing_entity params
else
return create_new_entity params
end
end
So far this feels icky - should this be done differently?
Coming from java spring, I would just define separate handlers on the controller like so:
@RequestMapping(method = RequestMethod.POST, params="submit=Action 1")
public ModelAndView action1(@RequestParam("selectedItemKey") String key) {
ModelAndView mav = new ModelAndView("action1");
//Business logic
return mav;
}
@RequestMapping(method = RequestMethod.POST, params="submit=Action 2")
public ModelAndView action2(@RequestParam("selectedItemKey") String key) {
ModelAndView mav = new ModelAndView("action2");
//Business logic
return mav;
}
Does rails provide something like this or am I thinking about this the wrong way?
Thanks for any suggestions
Upvotes: 1
Views: 283
Reputation: 50057
You could easily split it apart in two controller actions, but actually I do believe that in both cases you are creating a new Entity. So a POST :create
is the good REST way. And if you have to create a new item filled up with the given attributes, or create a new item as a copy from another does not really matter imho.
Upvotes: 0
Reputation: 6337
This does seem kind of bad. First of all, why are you copying entities in the first place? What's your use case?
Second, you're probably putting the logic in the wrong place. You should probably either (1) have separate create
and copy
controller actions, or (2) pass params
on to the model class and let it figure out whether it's creating or copying.
Upvotes: 1