I am building my first admin section in Rails and I am struggling with routing problems.
My routes.rb looks like this:
get "admin/menuh"
get 'admin/welcome'
namespace :admin do
resources :users
resources :menuh
resources :menuv
resources :welcome
end
And my views structure looks like views/admin/users/files. If I will set to url address of browser the url localhost:3000/admin/users/new, so I will get the error message No route matches {:controller=>"users"} (it's in the file views/admin/users/_form.html.erb - this file is generated by scaffold)... so I would like to ask you - where is the problem? Is here anything important, what I am disregarding?
You've set up your form_for like this, I reckon:
<%= form_for #user do |f| %>
Because the route is in a namespace, you need to tell the form that also:
<%= form_for [:admin, #user] do |f| %>
That should help you fix that issue.
Related
trying to make link to my recent uploaded image
<%= link_to (image_tag (post.image_url(:thumb))), post.image.url(:original), :class => 'postimage' %>
how ever it's not working, at all...
<% #post.image do |image| %>
<%= link_to (image_tag (post.image_url(:thumb))), post.image.url(:original), :class => 'postimage' %>
<% end %>
the fun part is that
<%= #post.image %>
works. but only shows /uploads/post/image/3/eKoh3.jpg
full code here https://gist.github.com/4332533
This line looks wrong to me:
<% #post.image do |image| %>
(btw your gist actually says #post.image.each do |image|, which I'm assuming is what you meant to do above)
If you are mounting an uploader on your Post model's :image attribute, then this makes no sense. A mounted uploader allows you to upload one image, and you can't iterate over it using each.
I'm not sure what you're trying to do. Are you trying to iterate over all the versions? Try post.image.versions.each
Are you trying to upload multiple images? Carrierwave can't help you with that directly. You'll need to create a new model, Image, and mount your uploader there. Your Post will need a line like
has_many :images
And your Image model will need to belong_to :post. You'll also need to figure out how to upload and manage images in that new table.
I'm building a basic app with Rails 3.2 and Devise 2.0. I've create a User devise model and a Projects model. In my routes.rb files I have
root :to => 'projects#index'
I can sign up and sign in at Get users/sign_up and Get users/sign_in, respectively, but when it redirects to projects#index, I don't see a notice at the top that says "You've signed in successfully. Which file do I need to check to fix this?
You need to add flash messages showing to the file /projects/index.html.erb
for example this way:
<% flash.each do |key, value| %>
<%= content_tag(:div, value, :class => "flash #{key}") %>
<% end %>
I have the following piece of code that I'm trying to fit into some generated scaffolding
= form_for(#event, :url => group_event_path(#event.group_id, #event) ) do |f|
As you can see, I've defined a nested resource route that looks like this
resources :groups do
resources :events
end
Now back to the form_for line above. The default Rails scaffolding uses code similar to above to generate _form, which is used in #new and #edit. The issue this presents to me is that form_for has to submit to these two paths
CREATE: group_events_path(#event.group_id)
UPDATE: group_event_path(#event.group_id, #event)
Is there a way for me to simplify this by modifying how the group_event(s)_path helpers work?
If you use the polymorphic form_for syntax, this will fix it:
= form_for([#group, #event]) do |f|
Now if that #event object is persisted in the database then it will use the update route, and if it's not then it will use the create route.
You can do the same thing with the normal form_for call:
= form_for(#event) do |f|
There's absolutely no reason to specify the :url option other than to customize the URL to be something different from what Rails infers.
I´m a newbie with Rails3 and I´ve got a strange problem. After searching in google and in StackOverflow for a while I decided to write down my question.
I have a Competencia and a Partida model. Competencia has_many :partidas and Partidas belongs_to :competencia.
I´m working with nested resources and my code looks like this:
routes.rb
resources :competencias do
resources :partidas
end
partidas_controller.rb
class PartidasController < ApplicationController
def new
#competencia = Competencia.find(params[:competencia_id])
#partida = #competencia.partidas.build
end
def create
#competencia = Competencia.find(params[:competencia_id])
#partida = #competencia.partidas.build(params[:partida])
if #partida.save then #blabla end
end
end
views/partidas/new.html.erb
<%= form_for [#competencia, #partida], :url => competencia_partidas_path(#competencia) do |f| %>
<!--blabla-->
<% end %>
I know that it isn´t the right way to specify the url in the form_for helper (specially if I´m not using a custom action), but it was the only way I could work it out. When I wrote something like this: <%= form_for [#competencia, #partida] do |f| %> I´ve got this error:
Showing /Users/ks/rails/projects/chronos/app/views/partidas/new.html.erb where line #4 raised:
undefined method `competencium_partidas_path' for #<#<Class:0x00000101718548>:0x00000101713728>
When I checked the routes (rake routes) everything seems to be fine.
competencia_partidas GET /competencias/:competencia_id/partidas(.:format) {:action=>"index", :controller=>"partidas"}
POST /competencias/:competencia_id/partidas(.:format) {:action=>"create", :controller=>"partidas"}
new_competencia_partida GET /competencias/:competencia_id/partidas/new(.:format) {:action=>"new", :controller=>"partidas"}
Can someone explain me where the competencium name comes from?. What would be the correct approach to solve this?
The problem is that Rails assumes english grammar rules for pluralization. You can read more here: http://api.rubyonrails.org/classes/ActiveSupport/Inflector.html. You can customize the inflector or use English model names. I'm Italian and even when a project is meant only for Italian customer I prefer to use English names.
Rails tries to singularize you model name. In your case, it thinks competencia is the plural of a latin word. To add an exception, put the following in config/initializers/inflections.rb:
ActiveSupport::Inflector.inflections do |inflect|
inflect.singular "competencia", "competencia"
end
I just discovered the rails-settings gem and now I need to make an admin page that lets me edit the setting values. How would I make a settings controller with an edit view that can change these dynamic app wide settings?
I haven't used this gem but it seems like it should be fairly straight forward. Since it uses a database backed model, you would simply create a controller as normal:
rails g controller Settings
From here you would define your index action to gather all your individual settings for display in the view:
def index
#settings = Settings.all
end
Then in the view you can setup a loop to display them:
<% #settings.each do |setting| %>
<%= setting.var %> = <%= setting.value %>
<% end %>
As far as editing ... this might be a bit tricky since by default rails would expect you to submit only one setting at a time to edit. You could do it this way but unless you implement the edit with ajax it might be tedious and non-intuitive.
Another way would be to set up your update method to accept all the individual settings at once, loop through and update each one with new values. It might look something like this:
// The /settings route would need to be setup manually since it is without an id (the default)
<%= form_tag("/settings", :method => "put") do %>
<% #settings.each do |setting| %>
<%= label_tag(setting.var, setting.var) %>
<%= text_field_tag(setting.var, :value => setting.value) %>
<% end %>
<%= submit_tag("Save Changes") %>
<% end %>
This should output all of the settings (given they have been assigned to the #settings variable) with the var name as the label and the current value as the text field value. Assuming that the routing is setup, when you submit this form the action that receives it should all the new settings in the params variable. Then you can do something like this in the action:
def update
params.each_pair do |setting, value|
eval("Settings.#{setting} = #{value}")
end
redirect_to settings_path, :notice => 'Settings updated' # Redirect to the settings index
end
This may not be the best way depending on how often you edit the settings and how many settings you have...but this is a possible solution.
I was looking for some suggestions for this and found another answer to this that is very simple and elegant, for anyone looking for this later. It just sets up dynamic accessors in your model, allowing your form to have settings fields just like your normal attributes. An example can be found in the original answer:
How to create a form for the rails-settings plugin