简体   繁体   中英

Rails: How to choose which js file a controller action to render?

I know the fact that by default Rails chooses the javascript file depending on action's name to render:

For example if I have

def create
     #Code and Stuff happening here
end

and here I have

create.js.erb

which will be ran by default

But is there a way that I can choose a different javascript file to be rendered from inside the action ?

For example

create1.js.erb
create2.js.erb

You'll want to add something like the following:

respond_to do |format|
  format.js { render :create1 }
end

Alternately, if you had a different path to it besides the default you could use:

format.js { render :file => "/path/to/save.js.erb" }

If it is a more dynamic requirement than this, you could always use string interpolation or "string".to_sym to render the desired view.

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