简体   繁体   中英

how can I send a dynamic if for redirect_to in rails

Can someone help me with redirect_to with a dynamic id

I want to redirect a page with the id from the variable which is set in params

I want

format.html { redirect_to '/classrooms/7', notice: 'successfully updated.' }

so 7 is my id like classroom/[:id]

I tried to use redirect_to in my controller but din't work

@classroom_id = params[:classroom_id]    
@backtoclassroom = "/classrooms/@classroom_id"

and

format.html { redirect_to @backtoclassroom, notice: 'successfully updated.' }

Thanks

Why don't you just use the string interpolation? Which you actually tried to use but it didn't work because you are not using #{} so your instance variable is not a variable anymore but a literal string. Use something like

redirect_to "/classrooms/#{@classroom_id}"

Also, make sure you use prepared MySQL statements if you are going to use the user input to query your DB records.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM