What is the right way set up address for form? - ruby-on-rails

I have controller my_controller and in it the action my_action. In my_action view I created a form and manually set this: <%=form_tag('/photos/create') do%>. When I send this form to this action in the photos controller, I'll get the error
No route matches [POST] "/photos/create"
(Obviously the action create exist in the photos controller)
My question is, why I am getting this error, when this action exist and, how can I fix it (what is the right way to set up address in the for manually)?

Because your route is not correct. The create action for photos should be to the /photos route instead with a POST. You can verify this by running rake routes at your command line to get a list of all routes and how they are mapped to your controller actions.
Try this instead:
<%=form_tag(photos_path, :method => :post) do%>
For more information: http://guides.rubyonrails.org/routing.html

Related

Rails - calling a method in controller using link_to

I'm trying to call a method in controller from my index.html file while staying on the same path... is it possible... can anyone help me
index.html
<%= link_to "Click", controller: "products", action: "click" %>
controller.rb
def click
puts "click called!!!"
end
routes
get 'click'
Error: Missing :controller key on routes definition
Per the documentation of Rails Routes, you got to specify the controller and the action where it is routed, in your case the Products controller within the click action.
So in your routes.rb file your proper get syntax should include the destination, like this:
get '/products/:id', to: 'products#click'
Also in your link_to you should include the ID of the product so the routing passes the id to the controller and you should include logic in your controller to manipulate that ID how you need. See this.
PS: Don't name your action click , name it as the actual action to perform on a product, like show. Please also check what Resources routing is.

How to create a custom route when calling an action using form_for?

I created custom routes '/album' that routes to 'microposts#new'. However, when I use a form_for it routes to '/micropost' instead of returning to '/album'.
In my controller I'm using render 'album'. I would prefer to keep as render, although I can make it work using redirect_to.
I know the problem is when I'm calling the create action in my micropost controller, it goes to /micropost instead of /album. Still new to rails and help would be appreciated.
Here is routes.rb:
root 'pages#home'
get '/album', to: 'microposts#new'
post '/album', to: 'microposts#create'
Add url: "/album" as an additional parameter in the form_for.
For additional options, see the doc.

ruby on rails- non-existent route

I have a controller file,
abc_controller.rb.
I have defined the show method inside it.
I have a view file,
show.html.haml
inside app/views/abc/
In my routes.rb file, I am giving the following command
resources :abc
I have a button
= link_to 'abc', abc_path, class: 'btn btn-default'
But when I click on the button, its not going to the new page.
I am getting non-existent route error.
Since I am a newbie to rails, I am not able to figure what the problem is.
You are getting a error because there is no such path as abc_path. Run rake routes and you'll see the routes that Rails understands. In you example, resources :abc produces the following routes.
abc_index GET /abc(.:format) abc#index
POST /abc(.:format) abc#create
new_abc GET /abc/new(.:format) abc#new
edit_abc GET /abc/:id/edit(.:format) abc#edit
abc GET /abc/:id(.:format) abc#show
PATCH /abc/:id(.:format) abc#update
PUT /abc/:id(.:format) abc#update
DELETE /abc/:id(.:format) abc#destroy
The first column are the named routes. So in order to get to the index action of abc_controller, the route is named abc_index_path. There is an abc_path but it needs an id params which means that you need to pass something to it. In your case, there's no definitive value to pass to this method so as a trial just use abc_path(1) which will redirect you to /abc/1. This goes to the show action with params[:id] set to 1.
If you do resources (plural) the resulting route for show requires an id: /abc/:id(.:format), so abc_path requires that you pass that :id or a object. If you are dealing with a singular abc (resource :abc), the resulting path doesn't require that :id, so you code should work (this is less common, but hard to tell with your "abc" example.

Rails - No route matches POST

I have the following in my routes.rb file:
post 'report/mnps/generate' => 'report#mnps_generate', as: 'report_mnps_generate'
Then, in my reports/mnps.html.erb view I have this:
<%= button_to report_mnps_generate_path %>
However, this button redirects to a post method at reports/mnps. Why is this button redirecting there instead of report/mnps/generate?
EDIT
rake routes returns:
Prefix Verb URI Pattern Controller#Action
root GET / home#index
report_index GET /report(.:format) report#index
report_mnps GET /report/mnps(.:format) report#mnps
report_mnps_generate POST /report/mnps/generate(.:format) report#mnps_generate
The definition of button_to states that the first parameter is its name, which is usually used as label. See here:
http://apidock.com/rails/ActionView/Helpers/UrlHelper/button_to
To get a link to the page, you need to write the button like this
button_to('Clickme!', report_mnps_generate_path)
The reason why it loaded the page you stated is that the button is actually on that very same page and is simply reloading it since no other destination was defined in your button_to call.

Rails beginner config/routes.rb issue

I am having a little trouble understanding what config/routes actually routes to. For example lets say I started a brand new project and generated a Users controller and edited my config/routes.rb to look like this:
config/routes.rb
SampleApp::Application.routes.draw do
match '/signup', to: 'users#new'
end
I start the server and as expected, it says I don't have a "new" action in my Users controller. I create one:
users_controller.rb
class UsersController < ApplicationController
def new
end
I refresh the page and as expected it tells me I need a users/new template. So my question is: do my view templates always have to be the same as the controller and action names in "(controller name)/(action name)" format (in this case users/new.html.erb)? Is it not possible to name my template something random (e.g. users/rubyonrailsmeetup.html.erb instead of users/new.html.erb) if have a controller action linked to one of the site's functions?
Also, does adding "resources :users" to config/routes.rb by default match the view template file names with the behavior I mentioned above so that views must be named after their "controller/action" names?
Sorry for the bother, I'm just trying to figure out what's part of Rails' magic and what isn't.
Rails attempts to render the template with the same name as the action by default, if no other render or redirect is invoked in the controller action. Basically, there's an implicit render :action at the end of every controller action.
But you can override this easily enough by adding an explicit render, e.g.,
render :rubyonrailsmeetup
Edit for clarity: this call to render goes in the controller code, not in config/routes
do my view templates always have to be the same as the controller and action names in "(controller name)/(action name)" format
These are defaults, you can render any view from the action by giving render :view_file_rel_path at the end of the action
does adding "resources :users" to config/routes.rb by default match the view template file names
Anything added in routes.rb is directly related upto controllers only, i.e. it matches the request and maps it to the controller/action. Logic of view comes only inside the action code

Resources