1

rails generate scaffold controller sportler name:string
rails generate model einheit ... sportler_id:integer

/app/controllers/sportlers_controller.rb
..
def add_einheit
sportler = Sportler.find(params[:id])
@einheit = Einheit.new(:sportler => sportler)
render :template => "einheits/edit"
end
..

/app/views/sportles/index.html.erb - when i cut this source code below the route error
doesnet appear anymore

..

<td><br>
  <%= link_to "Trainingseinheit hinzufügen", :action => "add_einheit", :id => sportler %><br>
</td><br>

..

routes.rb

FITAPP2::Application.routes.draw do
resources :sportlers

Routing Error

No route matches {:action=>"add_einheit", :id=>#groesse: "3", created_at: "2012-12-27 15:56:04", updated_at: "2012-12-27 15:56:04">,
:controller=>"sportlers"}
Try running rake routes for more information on available routes.

1

4 Answers 4

1

As routing error suggest you should "try running rake routes for more information on available routes." The point is you obviously have not specify route rule for add_einheit method

Sign up to request clarification or add additional context in comments.

1 Comment

How can i define such a rule?
0
resources :sportlers 

This code will only creates routes about CRUD actions(create,new,edit,update vss...) To use "add_einheit" actions edit routes.rb ;

resources :sportlers do
    member do
        get "add_einheit"
    end
end

If you send a parameter like "id" use "member do" else use "collection do" in your routes.rb file.

2 Comments

after i did this, the error doenst occur but when i click on the link the view doesnt appear
That's because your template isn't in the right place for this action. Either move your template to 'sportlers/add_einheit' or force it to render your template using render :action => :edit, :controller => :einheits
0

Are you sure you have a html file under sportlers view folder, like view/sportlers/add_einheit.html.erb ?

3 Comments

no, but _form.html.erb, edit.html.erb, new.html.erb, show.html.erb
add_einheit is a method in my controller
your html file has to have the same name with your method, if you do not point a spesific html file in your method block. so if you have a add_einheit method in sportlers controller and you want to render a html file from this method, you set /sportlers/add_einheit.html.erb file.
0

I took Kaders solution. It functions but now i geht the next Routing Error. action => sportlers/update_einheit doesnt function. I dont understand the princip.

FITAPP2::Application.routes.draw do resources :sportlers do member do get "add_einheit" get "update_einheit" end end end

I try to extends the routes above with a second entry get update_einheit - it doenst function Routing Error

No route matches {:action=>"update_einheit", :id=>#, :controller=>"sportlers"} Try running rake routes for more information on available routes.

rake routes:

add_einheit_sportler GET /sportlers/:id/add_einheit(.:format) sportlers

add_einheit

update_einheit_sportler GET /sportlers/:id/update_einheit(.:format) sportlers

update_einheit

          sportlers GET    /sportlers(.:format)                    sportlers

index

                    POST   /sportlers(.:format)                    sportlers

create

       new_sportler GET    /sportlers/new(.:format)                sportlers

new

      edit_sportler GET    /sportlers/:id/edit(.:format)           sportlers

edit

           sportler GET    /sportlers/:id(.:format)                sportlers

show

                    PUT    /sportlers/:id(.:format)                sportlers

update

                    DELETE /sportlers/:id(.:format)                sportlers

destroy

1 Comment

If you took that solution, then please mark it as 'accepted' by clicking the arrow to the left of accepted answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.