My URL is : /students/information?name='john'
In my application, student is model, and information is not a model.
My routes is like this :
resources :students do
get :information, on: :collection
end
Can i make polymorphic_url for this URL?
In my stuent/index page, i have polymorphic_url like this. (For pagination links )
<%=
polymorphic_url(
Student,
name: params[:name]
)
%>
polymorphic_url([Student, :information], name: params[:name])
Related
Sometimes I won't have to provide a param to a route helper and it automagically pulls it in from the existing params. I can't seem to figure out how to get it to consistently work.
routes.rb:
scope ':admin_id', module: :admin do
resources :roles
end
When rendering a page where the :admin_id is set to 10:
<%= roles_path %> # /10/roles
<%= edit_role_path(my_role, admin_id: 10) %> # /10/roles/15/edit
<%= edit_role_path(my_role) %> # sometimes works
rails
routes:
roles GET /:admin_id/roles(.:format) roles#index
POST /:admin_id/roles(.:format) roles#create
new_role GET /:admin_id/roles/new(.:format) roles#new
edit_role GET /:admin_id/roles/:id/edit(.:format) roles#edit
role GET /:admin_id/roles/:id(.:format) roles#show
PATCH /:admin_id/roles/:id(.:format) roles#update
PUT /:admin_id/roles/:id(.:format) roles#update
DELETE /:admin_id/roles/:id(.:format) roles#destroy
Figured it out!
def default_url_options(options={})
{ admin_id: params[:admin_id] }
end
This will add the param to all of my route helper methods so I don't have to specify it each time.
I have a _form.html.haml partial for a model FileType that I want to render in my new.html.haml and edit.html.haml view.
= simple_form_for [:document_manager, file_type] do |f|
...
When I render this partial in the new view, this works, but when I render it in the edit view, simple_form tries to set the id of file_type to the locale param if my route. My routes look like this:
Rails.application.routes.draw do
scope '(:locale)', locale: locale: /#{I18n.available_locales.join('|')}/ do
namespace :document_manager do
resources :file_types, except: [:show]
end
end
end
For the new view the route /en/document_manager/file_types is generated, but when I try to access the edit view I get this error:
No route matches {
:action=>"update",
:controller=>"document_manager/file_types",
:format=>nil,
:id=>nil,
:locale=>#<FileType _id: 550198ca7462720388010000, user_file_id: nil, file_name: "...", description: "...">
} missing required keys: [:id]
How can I change the form that the id and locale parameter is set right for new and edit?
Update: A first solution that works is to remove the locale scope and store the language in the session, but for SEO, this will result in duplicate content, so a better solution would be having urls like:
/en/document_manager/file_types
/fr/document_manager/file_types
/de/document_manager/file_types
...
Use a normal path, some times rails overcomplicates, in this case it should automatically use the scope you are using but as you try to use it I have only used hardcoded scopes like /en|es|fr/.
If I were you I would use something like:
= simple_form_for document_file_type_path, method: :post do |f|
and use 2 different forms, try hardcoding your available locales like
scope "(:locale)", :locale => /en|es|fr/ do
resources :foos
resources :bars
...
EDIT
Also looks like it is waiting for 3 parameters try:
[:document_manager, "locale", file_type]
I have a user model, but I need to create a form to update only one attribute.
EDIT: I added the create_mailbox patch in :users resource but the form throws a undefined methodsettings_create_mailbox_path'` error. Can anyone give me some insight to how this member patch/resource routes work?
Here's the form:
<%= form_for #user, url: settings_create_mailbox_path(#user) do |f| %>
<%= f.text_field :rss_mailbox, autocomplete: 'off'%>
<button type="submit" class="button">Create Mailbox</button>
<% end %>
Here's the Users route:
resources :users, id: /.*/ do
member do
patch :settings_update, controller: :settings
patch :create_mailbox, controller: :settings
patch :view_settings_update, controller: :settings
patch :sharing_services_update, controller: :sharing_services
patch :actions_update, controller: :actions
end
end
And here's the settings route:
get :settings, to: 'settings#settings'
namespace :settings do
get :account
get :billing
get :import_export
get :feeds
get :help
post :update_credit_card
post :mark_favicon_complete
post :update_plan
post :font
post :font_increase
post :font_decrease
post :entry_width
end
It would depend on the attribute and what you're changing it to.
If it's a simple toggling of a boolean field, for example, you could get away with a link that fires off an ajax request, that makes the change and returns some HTML (eg. to update the link you clicked).
If it's something else, eg. a text field or a string or something, a form_for #user would be the typical way. But the form would only have one field, and the controller that processes the form post would have its strong parameters (or attr_accessible for Rails 3) set so that it will only accept data for that one field.
Was using the wrong route name. This was the right one:
create_mailbox_user_path
Rails 4 Use Strong Parameters to determine which attribute is allow to update like so:
def user_param
params.require(:user).permit(:attr)
end
Hi everyone I am doing a application with rails 3.2. I am trying to use form_tag but I have problem with the route.
I try this in my form:
= form_tag('/companies/save_category', method: "post") do
and this:
= form_tag({:controller => "companies", :action=>"save_category"}, method: "post") do
In my config/routes.rb:
I am a little confused to put route like this
resources :companies do
post 'save_category'
end
or route like this:
resources :companies do
member do
post 'save_category'
end
end
But either way does not work. And when I execute rake routes, I obtain the same result
company_save_category POST /companies/:company_id/save_category(.:format) companies#save_category
The error was this
No route matches [POST] "/companies/save_category"
any idea?
Consider these routes:
resources :companies do
member do
post 'save_category'
end
end
This member block means that the route save_category in the /compagnies/ namespace will need a Company id to work:
/compagnies/12/save_category # where 12 is params[:company_id]
Now, with collection:
resources :companies do
collection do
post 'save_category'
end
end
This means that to get to the save_category route, you don't need the company id:
/compagnies/save_category # will work, is not needing a params[:company_id]
In your case, you should first use the url helpers (generated following the routes.rb). You need here:
if save_category is a *member route*
save_category_company_path(#company)
elsif save_category is a *collection route*
save_category_companies_path
I guess the category you want to save is related to a specific company, right? If yes, you need a member route:
form_tag(save_category_company_path(#company), method: "post") do
Hope this helps!
I have the following route in my Rails3 project:
match "/blog/:permalink" => "posts#show", :as => :post
When I link to my post through a view as such:
<%= link_to #post.title, post_path(#post) %>
The id of the post is passed into the post_path helper (even though my route specifies the permalink is passed.
How do I force the post_path to send in the permalink instead of the id of the post?
I can explicitly call post_path(#post.permalink) but that seems dirty.
Am I missing something in my route?
Thanks!
Define a to_param method on the model that returns the string you want to use.
class Post < ActiveRecord::Base
def to_param
permalink
end
end
See this page, this Railscast, (and of course Google) for more info.
[Edit]
I don't think Polymorphic URL Helpers are smart enough to handle what you want to do here. I think you have two options.
1. Use a special named route and pass in the parameter similar to your question and Jits' answer.
match "/blog/:permalink" => "posts#show", :as => :post
and link to it
<%= link_to #post.title, post_path(:permalink => #post.permalink) %>
2. Create a new helper that generates the URL for you
match "/blog/:permalink" => "posts#show", :as => :post_permalink
and a helper
def permalink_to(post)
post_permalink_path(post.permalink)
end
and in your views
<%= link_to #post.title, permalink_to(#post) %>
Try something like this:
<%= link_to #post.title, post_path(:permalink => #post.permalink) %>
Rails should automatically construct the URL as per your routes (i.e. replacing :permalink accordingly).