My route looks like the following:
map.namespace(:admin) do |admin|
admin.resources :pages
end
and my controller name looks like the following:
class Admin::PagesController < ApplicationController
and my new.html.erb file looks like the following:
<% form_for(#page) do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :title %>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :body %>
<%= f.text_area :body %>
</p>
<p>
<%= f.submit "Create" %>
</p>
<% end %>
<%= link_to 'Back', :action => "index" %>
Yet I keep on getting the following error:
NoMethodError in Admin/pages#new
Showing app/views/admin/pages/new.html.erb where line #1 raised:
undefined method `pages_path' for #<ActionView::Base:0x104528000>
Extracted source (around line #1):
1: <% form_for(#page) do |f| %>
2: <%= f.error_messages %>
3: <p>
4: <%= f.label :title %>
I can't figure out why as I'm assuming the route is correct. If I try other routes then it will work until I try submitting the form, then it thinks it should be taking me back to site.com/pages which it shouldn't.
Any ideas?
Your model #page isn't aware that it's being used in a namespace like that. You can use rake routes to see all your routes for your admin namespace. You need to manually change your url path:
<% form_for(#page) do |f| %>
to
<% form_for(#page, :url => admin_pages_path) do |f| %>
Another example for when you're updating a page:
admin_page_path(#page)
Related
I keep getting an
undefined method 'orders_path' for #<#<Class:0x007faefee50a88>:0x007faefee414e8>
when navigating to my new order path url /users/1/orders/new
Been stuck on this, not sure what the deal is.
Routes:
devise_for :users, :path => 'accounts'
resources :users, only: [:index, :show] do
resources :orders
end
root index:
<%= link_to "Create an Order", new_user_order_path(current_user.id) %>
form:
<%= form_for([#user, #order]) do |f| %>
<%= f.hidden_field :user_id %>
<div class="form-group">
<%= f.label :name %><br />
<%= f.text_field :name, autofocus: true, class: "form-control" %>
</div>
<% end %>
new:
<h1>New Order</h1>
<%= render 'form' %>
<%= link_to 'Back', user_orders_path(#user) %>
When you write this:
<%= form_for(#order) do |f| %>
Rails is looking for orders_path, but you don't really have such a route as you have defined your orders resource nested under the users resource. So, you have to pass both: #user and #order to the form like this:
<%= form_for([#user, #order]) do |f| %>
# your code goes here
<% end %>
If you write the form this way, then Rails will look for this path: user_orders_path which will match your defined route and will work and you won't get this error anymore:
undefined method 'orders_path' for #<#<Class:0x007faefee50a88>:0x007faefee414e8>
So, this will fix your current issue. But, there is another issue in your new.html.erb file where you currently have this:
<%= link_to 'Back', user_orders_path %>
You have to pass the #user as an argument to this method:
<%= link_to 'Back', user_orders_path(#user) %>
otherwise, it will fail again.
Hope this helps to solve your problems.
Since orders is nested on top of users, the form_for declaration will need to include both the user, and the order objects.
<%= form_for([#user, #order]) do |f| %>
Your new template will also need to have a reference to the given user.
<%= link_to 'Back', user_orders_path(#user) %>
I am writing a simple form in rails 4.But url option is not working. I m getting this error
"No route matches [POST] "/articles/new"
My form
<h1>New Article</h1>
<%= form_for :article, :url => articles_path do |f| %>
<p>
<%= f.label :title %>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :text %>
<%= f.text_area :text %>
</p>
<p>
<%= f.submit %>
</p>
<%end %>
form_for works using prefix
The route which you want to reach is not having a prefix.
The prefix should have same name as the name of the controller.
You can give prefix using as attribute in routes file
Then there is no need to give url
Define the method manually after url option:
<%= form_for :article, url: articles_path, method: :post %>
I have following code in my edit view:
<%= form_for #content do |f|%>
<% if f.text_field :home %>
<%= f.label :Home %>
<%= f.text_field :home %>
<% elsif f.text_field :aboutus %>
<%= f.label :Abouts %>
<%= f.text_field :aboutus %>
<% end %>
<%= f.submit%>
Here #content contain following information:
id: "1", home: "staticcontent", aboutus: "staticcontent"
so i wants that if someone wants to edit home page he can see only home submission form and if he choose aboutus, he can see edit form page for only about us.suggest me whats correct way to use if,else in this block?
You could pass a URL param and work with that. For example:
# in some view
<%= link_to "Edit Homepage", edit_content_path(page: "home")
# this will link to /contents/:id/edit?page=home
Then in your form:
<%= form_for #content do |f|%>
<% if params[:page] == "home" %>
<%= f.label :home %>
<%= f.text_field :home %>
<% else %>
<%= f.label :about_us %>
<%= f.text_field :about_us %>
<% end %>
<%= f.submit%>
Notice I downcased the labels, and changed aboutus to about_us so that the label displays correctly.
This is probably simple, I'm still coming to terms with rails syntax. What is the right syntax to pass the address_id in the url for form_for to a modified route?
This is the form - note the "address_id parameter"
<div class="one_fourth floatcenter">
<%= form_for address, :url => edit_address_path(:id => address.id), :method => :get do |f| %>
<%= content_tag(:button, :class => 'btn btn-inverse') do %> Edit Address
<% end %>
<% end %>
</div>
And this is the route I've configured:
get "edit_address/:id" => "member/addresses#edit"
Id is not being passed to the controller for some reason...
form_for address should be enough if address is a persisted object, but if it's not enough, then form_for address, url: edit_address_path(address) is what you want.
This is very simple. In place of url, you put your post method route:
<%= form_for(#post, url: super_posts_path) do |f| %>
...
<% end %>
You also call by action
<%= form_for #friend,:url=> { action: "create_friend"} do |f|%><br>
<%= f.label :u_from %>
<%= f.text_field :u_from %>
<%= f.label :u_to %>
<%= f.text_field :u_to %>
<%= f.submit%>
<% end %>
Here's the page template I've got:
<h1>Editing user</h1>
<% form_for(#user) do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :forename %><br />
<%= f.text_field :forename %>
</p>
<p>
<%= f.label :surname %><br />
<%= f.text_field :surname %>
</p>
<p>
<%= f.label :address %>
<%= f.text_field :address %>
</p>
<p>
<%= f.label :postcode %>
<%= f.text_field :postcode %>
</p>
<p>
<%= f.label :contact_number %>
<%= f.text_field :contact_number %>
</p>
<% end %>
<%= link_to 'Show', #user %> |
<%= link_to 'Back', users_path %>
The controller is actually sub-typed from the AdminController as there's a separate section as the following class declaration shows:
class Admin::UsersController < ApplicationController
with the edit method as follows:
def edit
#user = User.find(params[:id])
end
With the error as follows:
undefined method `user_path' for #<ActionView::Base:0x104369fe8>
and the following in my routes:
map.namespace(:admin) do |admin|
admin.resources :pages
admin.resources :treatments
admin.resources :users
admin.resources :finances
end
So I'm a bit stuck, because it's the form_for(#user) that's doing it. I've seen this before but have no idea how to diagnose it unfortunately.
Since you are using a name spaced routes, you have to specify the name space in the form_for invocation.
<% form_for([:admin, #user]) do |f| %>
Refer to the documentation for more details.(Hint: Read the end of the Resource-oriented style section)
Doesn't the name-spacing change the named paths so maybe to need to be explicit it in the form like this?
form_for (#user, :url => admin_user_path) do |f|
For the namespace you are using....the link would be
<% form_for #user, edit_admin_user_path(#user.id) do |f|%>
.....
<% end %>
Do a rake routes to look at how routes look like for a namespaced controller