2016-08-19 2 views
1

값을 내 컨트롤러에 제출했습니다. 서버 로그에서 매개 변수 (: 스타일)가 전달되지만 데이터베이스에 삽입되지는 않습니다. 누군가 일어날 수있는 방법을 알고 있습니까?매개 변수가 form_for에서 전달되었지만 데이터베이스에 삽입/커밋되지 않았습니다.

서버 로그

Started POST "https://stackoverflow.com/users/25/cupboards" for ::1 at 2016-08-19 19:11:39 -0400 
Processing by CupboardsController#create as HTML 
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"asWeoSFSisr62oMOPXZnb//KJ8LnAhT1h07NOm1Yn2O8t06N4yrDGugkd01AMQcujYzSahH+O7bEA/jpH+L9fQ==", "style"=>"WEEKEND", "commit"=>"Make a new closet", "user_id"=>"25"} 
    User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 25]] 
    User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 25]] 
    (0.1ms) begin transaction 
    SQL (0.5ms) INSERT INTO "cupboards" ("user_id", "created_at", "updated_at") VALUES (?, ?, ?) [["user_id", 25], ["created_at", "2016-08-19 23:11:39.219003"], ["updated_at", "2016-08-19 23:11:39.219003"]] 
    (0.7ms) commit transaction 
Redirected to http://localhost:3000/users/25/cupboards/35/edit 
Completed 302 Found in 46ms (ActiveRecord: 1.4ms) 

형태

<%= form_for @cupboard, url: { action: "create" }, html: { class: "listform"} do |f| %> 
    <%= f.label "ATHLEISURE", class: "welcomelistitem" %> 
    <%= radio_button_tag(:style, "ATHLEISURE") %> 

    <%= f.label "CASUAL", class: "welcomelistitem" %> 
    <%= radio_button_tag(:style, "CASUAL") %> 

    <%= f.label "PROFESISONAL", class: "welcomelistitem" %> 
    <%= radio_button_tag(:style, "PROFESSIONAL") %> 

    <%= f.label "WEEKEND", class: "welcomelistitem" %> 
    <%= radio_button_tag(:style, "WEEKEND") %> 

    <%= f.label "FESTIVAL", class: "welcomelistitem" %> 
    <%= radio_button_tag(:style, "FESTIVAL") %> 

    <%= f.label "DATENIGHT", class: "welcomelistitem" %> 
    <%= radio_button_tag(:style, "DATENIGHT") %> 

<%= f.submit 'Make a new closet' %> 
<% end %> 

컨트롤러

def create 
    @user = User.find(params[:user_id]) 
    @cupboard = @user.cupboards.new(cupboard_params) 
if @cupboard.save 
    redirect_to edit_user_cupboard_path(@user, @cupboard) 
else 
    render :new 
end 
end 

에 Params

private 
    def cupboard_params 
    # I used .fetch because .permit wasnt working. workaround found at http://stackoverflow.com/questions/24944871/actioncontrollerparametermissing-param-is-missing-or-the-value-is-empty-film 
    params.fetch(:cupboard, {}).permit(:style, :season, :neutral1, :neutral2, :accent1, :accent2) 
    end 
end 

도움을 주셔서 감사합니다!

답변

1

이 발생합니다. form_for 방법을 form_tag 것과 혼합하는 것 같습니다. radio_button_tagradio_button으로 바꿔야합니다. 또한 표기법에는 두 가지 형식이 있습니다. 함수 호출 radio_button(:model_name, :col_name, 'value') 또는 양식 객체 자체에 대한 메소드 호출 f.radio_button(:col_name, 'value')입니다. 양식의 또 다른 문제는 각 라디오에 대한 레이블을 사용하는 반면 모든 라디오 버튼 그룹에 대해 한 번 사용하도록 설계되어 있다는 것입니다. 따라서, 형태는 다음과 같아야합니다

def cupboard_params 
    params.require(:cupboard).permit(:style) 
end 

는하지만,이 작동하는지 확인하는 시간이 그랬던 것처럼 보인다

<%= form_for @cupboard, html: { class: 'listform' } do |f| %> 
    <%= f.label :style, class: 'welcomelistitem' %> 
    <%= f.radio_button :style, 'ATHLEISURE' %> 
    <%= f.radio_button :style, 'CASUAL' %> 
    <%= f.radio_button :style, 'PROFESSIONAL' %> 
    <%= f.radio_button :style, 'WEEKEND' %> 
    <%= f.radio_button :style, 'FESTIVAL' %> 
    <%= f.radio_button :style, 'DATENIGHT' %> 

    <%= f.submit 'Make a new closet' %> 
<% end %> 

당신은 다음과 같이 cupboard_params 방법을 다시 작성해야합니다 그것은해야한다.

+0

감사합니다. 도움이되었습니다. 태그를 제거한 것은 내 radio_button_tag였습니다. 그리고 f. 도움에 감사드립니다! – IWI

+0

@Samy 굉장! 천만에요. – dskecse

1

"style"매개 변수는 params[:cupboard] 내부로 전달되지 않습니다. 바로 params에 있습니다.

나는 내가 거기에 무슨 일이 일어나고 있는지 확실하지 않다 귀하의 양식 (스타일 외에) 다른 PARAMS를 볼 수 있지만이

def cupboard_params 
    params.permit(:style) 
end 

을 시도하거나이에보기에서 라디오 버튼을 변경하지 마십시오 양식이 style 대신 cupboard[style] 같은 style 속성 값을 제출하기 때문에

radio_button_tag('cupboard[style]', "ATHLEISURE") 
관련 문제