Rails does not navigate to controller method - ruby-on-rails

I am trying to add a new controller method. I also created a corresponding view for the same. All I am trying to do is create a variable called #x and printing it in the view. I am doing this just to verify if my application is going into the method. However, I do not see the value getting printed on the view. Please help.
This is my controller named submitted_content_controller.rb
class SubmittedContentController < ApplicationController
def begin_planning
#x = 1
end
end
This is my view called begin_planning.html.erb
<%= form_tag url_for(:action => :revision_planning), method: "post" %>
<%= text_field 'questionnaire', 'name', class: "form-control width-250" %>
<p><%= #x %></p>
<button>Create</button>
This is the routes I added:
get :begin_planning
When I am in the begin_planning html page, I see the text field and create button but I don't see the value of #x getting printed. It will be a huge help. Thank you.
This is my routes output:
begin_planning_submitted_content_index GET /submitted_content/begin_planning(.:format) submitted_content_controller#begin_planning
there are other routes to which I didn't add, because this controller has many other methods which I did not add since it's a huge file.

You may be rendering the view without going through the action.
If Rails finds a view file with a name that matches a route, but doesn't find a corresponding action for that route, it will still
render the view.
https://guides.rubyonrails.org/layouts_and_rendering.html#rendering-by-default-convention-over-configuration-in-action
So, you successfully render the form in the view, but Rails
doesn't know what #x means.
In your routes file, try adding the following outside of any namespaces:
get '/begin_planning', to: 'submitted_content#begin_planning'
Your rake routes should include the following:
begin_planning GET /begin_planning(.:format) submitted_content#begin_planning
And navigate to http://localhost:3000/begin_planning
Or,
if you'd like to namespace, you can add the following to your routes file instead:
namespace :submitted_content do
get :begin_planning
end
And your 'rake routes' should include the following:
submitted_content_begin_planning GET
/submitted_content/begin_planning(.:format)
submitted_content#begin_planning
And navigate to http://localhost:3000/submitted_content/begin_planning

Related

What action after "submit"?

In rails after pressing submit button in "new" I automatically come to "create" (similar after "edit" to "update"). All my params getting in "new" available in "create". This is by default.
If I have "export" instead of "new" and "process_export" instead of "create" - how reach similar effect?
If I get you right than you don't want to send the form to the default create action but rather to another, in your example to a process_export action.
All you have to do is to create a route in your routes.rb and set a custom form action url.
Example:
routes.rb
get '/process_export' => 'your_controller#export' # replace `your_controller` with your controller name
post '/process_export' => 'your_controller#process_export', as: :process_export
In your export view:
<%= form_for :resource, url: process_export_path do |f| %> <!-- Replace resource with your proper resource -->
<!-- Your form here -->
<% end %>
But I highly recommend you to observe the conventions of REST. It makes your live a lot easier.
Here are two resources which explain it in more detail:
Rails Routing: http://guides.rubyonrails.org/routing.html
Form Helpers: http://guides.rubyonrails.org/form_helpers.html
Rails follow the Convention over Configuration concept . The 7 methods of REST are automatically called from the controller when a similar request comes in the Rails 4 . For ex : the GET request will call new method , POST will call create method , DELETE will call destroy method etc .
Now , if you are creating a custom method in your controller like export and if you want to call it after the clicking on the submit button , you have to set routes accordingly in the routes.rb file . Which can be done as :
post "/chats" => "chats#export"
Here , chats is the controller and export is the method in that controller which you want to call on the submit action.
You can do :
$ > rails g scaffold Controller_name
and this will generate all the 7 REST methods in your controller automatically and similar routes are generated which you can check by doing :
$ > rake routes
I hope this helps.

undefined method `pushes_path' for #<#<Class:0x007f85a15c6c90>

i'v been trying to resolve this error for the past 5 hours and I'm gonna burn my computer if I can't solve this.
undefined method `pushes_path' for #<#:0x007f859d605250> this is the error code I'm getting but i don't understand why.
this is my index.html.erb file inside of the interaction
<%= simple_form_for #push do |f| %>
<%= f.input :payload, as: :text %>
<%= f.input :segment, as: :radio_buttons %>
<%= submit_tag "start the campaign" %>
<% end %>
and this is my interaction controller
class InteractionController < ApplicationController
def index
#push =Push.new
end
end
Push is my table in the database and i'll get the inputs and write them in the database to use them for later one.
and this is my routes file
devise_for :partners
get 'home/index'
get 'segmentation/index'
get 'interaction/index'
root to: "home#index"
i really don't know why its looking for pushes_path, what am i doing wrong?
form_for
The problem you have is that your form_for method is going to try and generate a route based off your #path object. And as such, if you don't have a path created for it, you'll receive the error you're getting:
:url- The URL the form is to be submitted to. This may be represented
in the same way as values passed to url_for or link_to. So for example
you may use a named route directly. When the model is represented by a
string or symbol, as in the example above, if the :url option is not
specified, by default the form will be sent back to the current url
(We will describe below an alternative resource-oriented usage of
form_for in which the URL does not need to be specified explicitly).
The bottom line is that as Rails is object orientated, its built around the assumption that you'll have routes set up to handle the creation of individual objects.
Every time you use form_for, Rails will attempt to construct your routes from your object -- so if you're trying to do the following, it will treat the routes as photo_path etc:
#app/views/pushes/new.html.erb
<%= form_for #push do |f| %>
...
<% end %>
--
Fixes
As #mandeep suggested, there are several fixes you can employ to get this to work:
Firstly, you can just create a route for your push objects:
#config/routes.rb
resources :pushes
Secondly, as you're using a different controller, you'll want to do the following:
#config/routes.rb
resources :interactions
#app/views/pushes/new.html.erb
<%= form_for #push, url: interaction_path do |f| %>
...
<% end %>
This will route your form submission to the interactions controller, rather than the pushes controller that you'll get by default!
Objects
Something to consider when creating Rails-based backends is the object-orientated nature of the framework.
By virtue of being built on Ruby, Rails is centered on objects - a term for a variable, which basically encompasses much more than just a piece of data. Objects, in the case of Rails, are designed to give the application:
Once you understand this, the entire spectrum of Rails functionality becomes apparent. The trick is to realize that everything you do in Rails should be tied to an object. This goes for the controllers too:
--
Ever wondered why you call resources directive in your routes, for a controller? It's because you're creating a set of resourceful routes based for it:
Do you see how it's all object orientated?
This gives you the ability to define the routes for specific controllers etc. The most important thing to note is how this will give you the ability to determine which routes / controller actions your requests should go
--
There's nothing wrong in using the controller setup as you have - the most important thing is to ensure you're able to define the custom URL argument, as to accommodate the non-object based structure
In your index action you have
def index
#push =Push.new
end
and your form has
<%= simple_form_for #push do |f| %>
so your form is looking for /pushes with post verb or pushes_path and you don't have that route in your routes.rb file so to fix this you need to add this in routes.rb:
resources :pushes
Update:
when you add resources :push rails basically creates seven different routes for you. One of which is
POST /pushes pushes#create create a new push
and if you look at the html generated by your form it would be something like:
<form action="/pushes" class="new_push" id="new_push" method="post">
// your fields
</form>
notice the action and verb so when you submit your form your routes are checked for them and since you didn't define them in your routes you were getting this error
And how will i be able to use the params i m getting from this form with this new resource addition?
Your form will take you to pushes_controller create action so first of all you'll have to define them. You can access them simply by params[:pushes] in your controller action but since you want to create a new record so you'll have to permit those attributes, checkout strong parameters
If you are using rails >= 4 then you can do
class PushesController < ApplicationController
def create
#push =Push.new(push_params)
if #push.save
redirect_to #push
else
render 'interaction/index'
end
end
private
def push_params
params.require(:push).permit(:attributes)
end
end
If you are using rails < 4 then instead of permitting these attributes(because strong parameters feature came from rails 4) you'll have to tell rails that these attributes are accessible by writing this in your pushes.rb
attr_accessible :attribute_name
Why it is assuming that its pushes controller?Because of the Push.new creation?
That's because if you look at your index action #push = Push.new so #push contains a push object with nil values(as you have just initialized it) so this is where rails magic comes, rails automatically tries to figure out url of your form and since your #push is only an initialized variable so rails takes you to create action for it. For details you should checkout rails polymorphic urls If you want your form to go to interaction_controller or some other url then you'll have to specify the url option for it
<%= form_for #push, url: "your_url_for_custom_method" %>
// other fields
<% end %>
And in the end you should really read docs

Render view from application_controller

So, I have search form, and search is avaliable obviously from any page.
I thought that it makes sense, that such action from application controller is placed in layouts/views folder.
But I just don't get- Rails doesn't see it. So I can't do this? How then should I provide action, avaliable from any page?
Code:
def tests_search
#tests=Test.test_search(params[:query])
respond_to do |format|
format.html
end
end
Route:
search_tests GET /search_tests(.:format) application#tests_search
Form:
<%= form_tag search_tests_path, {:id=>'test_search',:method => :get} do%>
Error:
Unknown action
The action 'tests_search' could not be found for ApplicationControllerr
You should create a new search controller. Use rails g controller search index which will create a search controller with a index action (you could also call the action something like result). Then add a search/_form.html.erb file in your search view folder, with the form:
<%= form_tag search_path, {:id=>'test_search',:method => :get} do |f| %>
and render this in your layout/application.html.erb where you want it to be:
<%= render "search/form" %>
This way you have a search form on any pages, that uses the SearchController to handle the search requests.
I would recommend using other controller to do this. It can be for example SearchController even if there will be only one method.
Notice that ApplicationController is controller that every other controller in application inherits from by default. So if it wouldn't be the case, it could make sense, but now every controller will inherit your test_search action, which is not desired.
If your search form will be a partial, then there is no difference whether this is in ApplicationController or in any other controller. You just have to point to right route.
Initially you have to explain yourself the flow. What you need is some partial which is rendered on all pages, and if a user adds some input to it and submits, he gets some output. Right? Good. So you start by creating a new partial somewhere in
app/views/shared/_search.html.erb
Then, you create your route in routes.rb to point to a controller's action. You don't have to place this in application_controller. Instead, create your search_controller.rb and create some action which responds to the form submission there.
Whenever you want to render your search form on other pages, you simply call render partial (more on that here) with something like
<%= render "shared/search" %>
This is good if you created the file above. Make sure your action exists and the name is correct, in your case it should be:
def test_search
...
end
Good luck.

link_to custom action but wrong method?

all, I'm trying to get a custom action to work with a put method: in the
in _post.html.erb i have a link_to statement:
<%= link_to 'End now', post, :method => :put, :action => endnow %>
routes.rb contains:
resources :posts do
member do
put :endnow
end
and posts_controller.rb looks like:
class PostsController < ApplicationController
helper_method :endnow
[.. code for create, edit, destroy, etc ..]
def endnow
puts params
end
end
rake routes's relevant line looks like:
endnow_post PUT /posts/:id/endnow(.:format) posts#endnow
However, the action endnow helper doesn't run when clicking on this link.
Strangely, it does run with an index action (which i can tell from the puts command.
Of course, eventually the code for endnow will update #post, but for now, it just doesn't run properly.
Maybe i'm going about this the wrong way - all I'm trying to achieve is to update #post upon clicking the link to that post, and before showing it.
Any ideas / Alternatives?
Why not use the route helper method provided to you? Change your link to
<%= link_to 'End now', endnow_post_path(#post), method: :put %>
Things you're doing wrong:
If you want to specify the :action, use the Symbol for the action (you're missing a colon). :action => endnow should be action: :endnow
I will assume you have a #post instance variable you're passing from your controller to your action. You should be using that instead of post (unless you do in fact have a local post variable you're omitting from your code)
You are using endnow as an action; you should remove the helper_method :endnow line in your controller because it's not something you want to/should be accessing from your view.
This can all be avoided by using the route helper (for endnow_post you'd append _path to get the local route path: endnow_post_path), and pass in your #post as an argument.
Because you're trying to do a PUT request, you must make sure you have something like jquery-ujs included in your asset pipeline to convert these links to form submissions behind the scenes; browsers don't support PUT via the click of a link on their own.
As for why you're getting the template error when you get your link_to working, Rails is telling you that you need to create a app/views/posts/endnow.html.erb file. Your action has only puts params which does not terminate execution, leaving Rails to assume you still are trying to render some endnow.html.erb template.
Are there other ways to do what you're trying to do (change a single attribute of a specific model)? Sure. Are there better ways? That's pretty subjective; it may not be the most RESTful way, but it's arguably easier to deal with (if for example there are very specific authorization rules to check before updating the attribute you are modifying in endnow. Does the way you've started fleshing out work? Absolutely.
Finally, as a bump in the right direction, after you fix your link_to and remove the the helper_method as I have described above, your endnow action might look like this:
def endnow
post = Post.find!(params[:id])
post.some_attribute_here = some_new_value_here
post.save
redirect_to :root and return # <- this line sets a redirect back to your homepage and terminates execution, telling rails to do the redirect and **not** to render some endnow.html.erb file
end

simple link_to in rails

I have a page app/views/new/news.html.erb and I simply want to link to this page from within my layouts/application.html.erb. Can someone please help! I have been struggling with this all morning.
I have tried things like <%= link_to "News", ... But I'm not really sure where to go from there.
You don't "link" to a view, you link to a controller which renders that view. Then, you'll need an entry in your routes.rb to wire up the url routing for that controller. If you have a controller named NewsController with a method called index and an entry in your routes.rb that looks like resources :news the following link_to should work: link_to "News", news_path.
In case it's not clear, the index method in your NewsController needs to have render :news in it.
Sounds like you may want to check out the guide on this topic: http://guides.rubyonrails.org/routing.html
If you have it setup correctly you should have a plural for your controller (i.e. news instead of new) and the following should work:
<%= link_to 'News', :controller => "news", :action => :news %>
This is assuming you are using scaffold.
If are adding this folder and page manually: for dynamic page, you have to create an action in your controller. For static page, you have to put it in your public folder.
You can always run
rake routes > routes.txt
in your application directory, which will dump a list of all routes into a txt file. Choose path that leads to action and controller you want, and then supply it as a param for link_to method :)

Resources