2013-04-27 4 views
0

해시에 키와 값을 명시 적으로 설정 한 다음 선택에 사용하려면 어떻게해야합니까? 외부 링크를 저장할 수있는 링크 모델이 있습니다. 링크에는 제목과 URL이 있습니다. 제목으로 키를 사용하고 url을 값으로 사용하고 싶습니다.선택에 대한 키와 값을 명시 적으로 설정

--------------------- 업데이트 ------------------------ -

이 작품 ..

links = Link.all 

link_array = [] 

links.each do |link| 
    link_array << [link.title,link.url] 
end 

하지만, 지금 여기 히치입니다. 이 배열을 다른 배열에 결합하여 두 모델을 단일 양식 선택에서 선택할 수 있도록하고 싶습니다. 이처럼 ...

a = PagesController.action_methods 
    # this grabs each action from the pages controller that will later be used as a route 

b = a.select {|s| s.include? "callback"} 
c = a - b 
    # b and c removes any position in the array that includes the word 'callback' so that only actions defined in the controller are returned 

links = Link.all 

link_array = [] 

links.each do |link| 
    link_array << [link.title,link.url] 
end 

@all_links = c + link_array 
    # desired result is an array used in a single form select containing both external links and internal links 

답변

0

이 작업은 원하는 작업을 수행합니다. 아마 더 좋은 방법이있을 것입니다.

a = PagesController.action_methods 
b = a.select {|s| s.include? "callback"} 
c = a - b 
@c_array = [] 
c.each do |p| 
    @c_array << [p, "#{p}_path"] 
end 

links = Link.all 
@link_array = [] 
links.each do |link| 
    @link_array << [link.title, link.url] 
end 

@all_links = @c_array + @link_array 

내부 경로 (페이지 작업에 대한)와 URL에 대한 외부 링크를 모두 포함하는 단일 배열이됩니다. 이제 세 번째 모델에서 pathto라는 제목의 문자열 열을 가질 수 있으며 경로 (문자열) 또는 URL을 저장합니다. 그런 다음 도우미에서이 작업을 수행하십시오.

def send_route(pathto) 
     if pathto.include? "http" 
     pathto # just go to the url 
     else 
     self.send(pathto) # create a route from the string stored in the db .. i.e. "home_path" 
     end 
    end 
0

나는 당신이려고하고 정확히 모르겠지만,이 같은 뭔가를 찾고있는 것처럼 보인다 :

Link.where(title: url) 

... 어디 URL 일부 로컬 변수입니다. 이렇게하면 다음과 유사한 SQL 문이 생성됩니다.

SELECT * FROM links WHERE title='yoururl' 
관련 문제