Is it bad to leave unused REST actions?

I am really confused about how to do this.

I'm tired and disappointed too, and my brain is sick looking at so many different things.

  • Is it a bad practice to leave unused REST actions? For example, I have a Resource model, and users can submit them, but I do not need the "index" action to show them, because this is handled by the Home # index controller. All I want to do is use link_to to go to the form page to add a new resource. So now all the link to im is trying to try going to / resources when I do resource_path. I read how you can use the "obsolete" methods and just refer directly to the action in the controller, but this is "not the way of the rails." now I'm trying to figure out what the rails path is if im doesn't use all the different REST actions that it does by default.

  • Does the script look like I'm doing it right? what I wanted it to be in home index # to list all resources, not # index resources?

  • If this is bad practice, then how can I create them manually? all the tutorials that I see online, "just type resource :user and you are good! He does not say what to do if you do not want / need all of them.

  • What if I want to change the default url? I don't particularly care about this, but everyone at the IRC tells me that this is "wrong." How to change the URL incorrectly? I want it to be / resource / submit, not / resources / new. The singular / plural just seems more useful to me and looks more professional.

  • Given that I leave all URLs the same. If I go to / resources / new and click submit in the form that says

Missing template

 Missing template resources/create, application/create with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee]}. Searched in: * "/Users/Zesty/Code/gem-portal/app/views" * "/usr/local/lib/ruby/gems/1.9.1/gems/devise-2.0.4/app/views" 

If I have resources: resources on my routes, I have no idea why it does not automatically go to # create resources:

  def create @resource = Resource.new(params[:resource]) end 

Thanks!

That's what is on my routes, by the way, in the event that something ridiculous is wrong

  root :to => 'home#index' resources :resources # match '/resource/submit', :to => 'resources#new' match '/learn', :to => 'pages#learn' match '/contact', :to => 'pages#contact' match '/requests', :to => 'pages#requests' 
+6
source share
1 answer

1. Inefficient practice to leave unused REST actions?
As far as I can answer this. If you do not need an index action for the controller, you do not have it, period. But what you should exclude this action in your routes, for example. by making resource :resources, :except => [:index] . This can be useful when you save from errors because NoTemplateError is not the same as RoutingError (which means you can skip something in production).

It is also a good development assistant. When you accidentally write a link to the index of an unused controller, you will receive an error message on the page containing the link if you excluded the action on your routes. If you do not, you will see an error message after clicking the link, because the route is valid (but does not have a function / template / in general).

2. Does the above script make it as if I am doing it right? what I wanted it to be in home index # to list all resources, not # index resources?
I cannot answer this question without understanding your business logic / application workflow. But if you do not need it, you will leave it.

If this is a cosmetic issue (for example, the # resource index is mostly needed, but under a different URL), you can simply create a match "home", :to => "resources#index" route match "home", :to => "resources#index" . This will reduce your user home controller, and you can use the index action on resources , which will be available in /home and under /resources . But then again, this may not be the best option for your workflow / logic, so this is just a suggestion.

3. If this is bad practice, how can I create them manually?
There is a great page on the Rails page about all your routing needs. Look over there.

4. What if I want to change the default URL?
Rails are a conditional configuration. Means that these methods are rails. It’s good to adhere to this agreement, so every time you return to your application, change things, you know, at least, how it works because of this agreement. Even rail encoders that don't know your application can make changes to it faster when everyone adheres to this agreement. This interesting commercial offer by RoR vs PHP explains it very well.

You can change them, but you should not.

5. Given that I leave the entire URL the same. If I go to / resources / new and click submit in the form that says
After creating, you usually redirect to something or visualize a new action in the create call to save information about the current object (for example, errors) that would be lost during the redirection. You currently remain in the create action (without rendering / redirecting another action), so the rails expect the create.html.erb template.

Example:

 def create @resource = Resource.new(params[:resource]) if @resource.save # record valid and saved, so redirecting to /resources/:id redirect_to @resource else # record not valid. rendering the new.html.erb template # (HINT the new action is NOT called here). render :action => :new end end 

Keep in mind that .new creates a new object, but it is not stored in the database unless you call, for example, .save.


These are just my opinions and experiences on this subject, so if someone more experienced thinks that something is wrong, I am glad to read the comment and update my answer.

+12
source

Source: https://habr.com/ru/post/909441/


All Articles